• php-curl_init函数


    1、背景:

      近来做API开发,经常使用到curl模拟http请求(不要太依赖guzzle)。故整理了一下,具体代码如下!环境是在laravel5.4中,如果其他框架,请注意部分语法。

    2、源码:

    <?php
    class HttpCurl
    {
    public $ch = null; // curl handle
    private $headers = array();// request header
    private $proxy = null; // http proxy
    private $timeout = 5; // connnect timeout
    private $httpParams = null;
    public $error_info;


    public function __construct()
    {
    $this->ch = curl_init();
    }

    public function setHeader($header)
    {
    if (is_array($header)) {
    curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header);
    }

    // 'Authorization: Basic bGVhbm1vYmk6YjkwN2ViNTczODllNDI5Zjg1Njg2OTc2ZGNjMTQzNzQ='

    return $this;
    }

    public function setTimeout($time)
    {
    // 不能小于等于0
    if ($time <= 0) {
    $time = 5;
    }
    //只需要设置一个秒的数量就可以
    curl_setopt($this->ch, CURLOPT_TIMEOUT, $time);

    return $this;
    }

    public function setProxy($proxy)
    {
    if ($proxy) {
    curl_setopt($this->ch, CURLOPT_PROXY, $proxy);
    }

    return $this;
    }

    public function setProxyPort($port)
    {
    if (is_int($port)) {
    curl_setopt($this->ch, CURLOPT_PROXYPORT, $port);
    }

    return $this;
    }

    public function setReferer($referer = "")
    {
    if (!empty($referer))
    curl_setopt($this->ch, CURLOPT_REFERER, $referer);

    return $this;
    }

    public function setUserAgent($agent = "")
    {
    if ($agent) {
    // 模拟用户使用的浏览器
    curl_setopt($this->ch, CURLOPT_USERAGENT, $agent);
    }

    return $this;
    }

    public function showResponseHeader($show)
    {
    curl_setopt($this->ch, CURLOPT_HEADER, $show);

    return $this;
    }

    public function setParams($params)
    {
    $this->httpParams = $params;

    return $this;
    }

    public function setCainfo($file)
    {
    curl_setopt($this->ch, CURLOPT_CAINFO, $file);
    }

    public function get($url, $dataType = 'text')
    {
    if (stripos($url, 'https://') !== false) {
    curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($this->ch, CURLOPT_SSLVERSION, 1);
    }
    // 设置get参数
    if (!empty($this->httpParams) && is_array($this->httpParams)) {
    if (strpos($url, '?') !== false) {
    $url .= "&" . http_build_query($this->httpParams);
    } else {
    $url .= '?' . http_build_query($this->httpParams);
    }
    }
    // end 设置get参数
    //var_dump($url);
    //echo $url . PHP_EOL;
    curl_setopt($this->ch, CURLOPT_URL, $url);
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($this->ch);
    $status = curl_getinfo($this->ch);
    $this->error_info = [
    'error_no' => curl_errno($this->ch),
    'error_info' => curl_getinfo($this->ch),
    'error_msg' => curl_error($this->ch),
    'result' => $content
    ];
    curl_close($this->ch);

    if (isset($status[ 'http_code' ]) && $status[ 'http_code' ] == 200) {
    if ($dataType == 'json') {
    $content = json_decode($content, true);
    }

    return $content;
    } else {
    return false;
    }
    }


    /**
    * 模拟POST请求
    *
    * @param string $url
    * @param array $fields
    * @param string $dataType
    * @return mixed
    *
    * HttpCurl::post('http://api.example.com/?a=123', array('abc'=>'123', 'efg'=>'567'), 'json');
    * HttpCurl::post('http://api.example.com/', '这是post原始内容', 'json');
    * 文件post上传
    * HttpCurl::post('http://api.example.com/', array('abc'=>'123', 'file1'=>'@/data/1.jpg'), 'json');
    */
    public function post($url, $dataType = 'text')
    {
    if (stripos($url, 'https://') !== false) {
    curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($this->ch, CURLOPT_SSLVERSION, 1);
    }
    curl_setopt($this->ch, CURLOPT_URL, $url);
    // 设置post body
    if (!empty($this->httpParams)) {
    if (is_array($this->httpParams)) {
    curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($this->httpParams));
    } else if (is_string($this->httpParams)) {
    curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->httpParams);
    }
    }
    // end 设置post body
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($this->ch, CURLOPT_POST, true);
    $content = curl_exec($this->ch);
    $status = curl_getinfo($this->ch);
    $this->error_info = [
    'error_no' => curl_errno($this->ch),
    'error_info' => curl_getinfo($this->ch),
    'result' => $content
    ];
    curl_close($this->ch);
    if (isset($status[ 'http_code' ]) && $status[ 'http_code' ] == 200) {
    if ($dataType == 'json') {
    $content = json_decode($content, true);
    }

    return $content;
    } else {
    return false;
    }
    }

    public function put($url, $dataType = 'text')
    {
    if (stripos($url, 'https://') !== false) {
    curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($this->ch, CURLOPT_SSLVERSION, 1);
    }
    curl_setopt($this->ch, CURLOPT_URL, $url);
    // 设置post body
    if (!empty($this->httpParams)) {
    if (is_array($this->httpParams)) {
    curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($this->httpParams));
    } else if (is_string($this->httpParams)) {
    curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->httpParams);
    }
    }
    // end 设置post body
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($this->ch, CURLOPT_POST, true);
    curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "PUT");
    $content = curl_exec($this->ch);
    $status = curl_getinfo($this->ch);
    $this->error_info = [
    'error_no' => curl_errno($this->ch),
    'error_info' => curl_getinfo($this->ch),
    'result' => $content
    ];
    curl_close($this->ch);
    if (isset($status[ 'http_code' ]) && $status[ 'http_code' ] == 200) {
    if ($dataType == 'json') {
    $content = json_decode($content, true);
    }

    return $content;
    } else {
    return false;
    }
    }
    }

    调用:
    public static function apiGet()
    {
    $api_url = "url";
    $curl = (new HttpCurl);
    $is_success = $curl->setHeader([
    'content-type: application/json',
    'Authorization: Basic ' . self::AuthorizationCode(),
    ])->get($api_url);
    if ($is_success == false) {
    return $curl->error_info;
    }
    }
    
    
    public static function apiPost($data)
    {
    $api_url = "url";
    $curl = (new HttpCurl);
    $is_success = $curl->setHeader([
    'content-type: application/json',
    'Authorization: Basic ' . self::AuthorizationCode(),
    ])->setParams($data)->post($api_url);
    if ($is_success == false) {
    return $curl->error_info;
    } else {
    return $is_success;
    }
    }
    
    
    public static function apiPut($id, $data)
    {
    $api_url = "url";
    $curl = (new HttpCurl);
    $is_success = $curl->setHeader([
    'content-type: application/json',
    'Authorization: Basic ' . self::AuthorizationCode(),
    ])->setParams($data)->put($api_url);
    if ($is_success == false) {
    return $curl->error_info;
    } else {
    return $is_success;
    }
    }

    public statis function AuthorizationCode ()
    {
      //return base_64('auth:key');
    }
  • 相关阅读:
    Redis 7.0 新功能新特性总览
    adb实现钉钉自动打卡 MKY
    vue2+webpack 转 vite
    zsh: command not found:nvm 的解决方案
    SSH keys 生成
    sass(dart sass)和nodesass 的区别以及 /deep/、::vdeep的支持
    nvm(node的版本管理)简介以及nvm管理node的命令介绍
    处理 code.matchAll(...) is not a function 问题
    package.json 里面的~、^
    处理 vite 里面 __require() 方法报错
  • 原文地址:https://www.cnblogs.com/onlyzc/p/8416694.html
Copyright © 2020-2023  润新知