PHP的CURL的POST/GET访问

Posted 新太潮流

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP的CURL的POST/GET访问相关的知识,希望对你有一定的参考价值。

本文出至:新太潮流网络博客

/**
 * [curl以GET的方式访问]
 * @E-mial [email protected]
 * @TIME   2017-04-07
 * @WEB    http://blog.iinu.com.cn
 * @param  [data] $url  [访问的路径]
 * @param  string $data [description]
 * @return [type]       [description]
 */
function curlGet($url,$data=‘‘){
    $ch = curl_init();
    $header = "Accept-Charset: utf-8";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)‘);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $temp = curl_exec($ch);
    return $temp;
}

/**
 * [curl以POST的方式访问]
 * @E-mial [email protected]
 * @TIME   2017-04-07
 * @WEB    http://blog.iinu.com.cn
 * @param  [data] $url  [访问的路径]
 * @param  [type] $data [description]
 * @return [type]       [description]
 */
function curlPost($url, $data){
    $ch = curl_init();
    $header = "Accept-Charset: utf-8";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)‘);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);
    if (curl_errno($ch)) {
        return false;
    }else{

        return true;
    }
}

本文出至:新太潮流网络博客

以上是关于PHP的CURL的POST/GET访问的主要内容,如果未能解决你的问题,请参考以下文章

Curl和PHP - 如何通过PUT,POST,GET将json传递给curl

使用stream_context_create()模拟POST/GET请求的方法

PHP通用CURL类

PHP灌水机器人实现原理

PHP函数stream_context_create()模拟POST/GET

php中模拟post,get请求和接受请求详细讲解