PHP uses curl_init to initiate http requests to simulate login_PHP tutorial

WBOY
Release: 2016-07-13 16:57:03
Original
1230 people have browsed it

There are two types of http requests, one is our ordinary http request login, and the other is another https request login. Let me introduce to you in detail how to use curl_init to implement http and https login.

Note: To use the curl_init function, this php extension must be turned on.

1. Open php.ini and enable extension=php_curl.dll
2. Check which directory the extension_dir value of php.ini is, and check whether there is php_curl.dll. If not, please download php_curl.dll, and then copy libeay32.dll and ssleay32.dll in the php directory to c:/windows/system32. .

Initiate http request

The code is as follows Copy code
 代码如下 复制代码

function _http_curl_post($url,$data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
    curl_setopt($ch, CURLOPT_TIMEOUT,4);
         
    if($data){
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "value=".json_encode($data));  //请求参数转为json格式
    }
    curl_setopt($ch, CURLOPT_HEADER, false);
    $string = curl_exec($ch);
    curl_close($ch);
    return $string;
}

function _http_curl_post($url,$data)

{

$ch = curl_init();
 代码如下 复制代码

$params = array();
$params['id']       = 1
$params['web_name']   = '好脚本';
$params['web_url']    = 'http://www.bKjia.c0m/';
$params['web_miaoshu']      = '脚本编程示例';
$data = _curl_post($url,$params);
$arr =json_decode($data);

curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4); curl_setopt($ch, CURLOPT_TIMEOUT,4);                                                                             If($data){ curl_setopt($ch, CURLOPT_POST, 1);           curl_setopt($ch, CURLOPT_POSTFIELDS, "value=".json_encode($data)); // Convert request parameters to json format } curl_setopt($ch, CURLOPT_HEADER, false); $string = curl_exec($ch); curl_close($ch); Return $string; }
Call method
The code is as follows Copy code
$params = array(); $params['id'] = 1 $params['web_name'] = 'Good script'; $params['web_url'] = 'http://www.bKjia.c0m/'; $params['web_miaoshu'] = 'Script programming example'; $data = _curl_post($url,$params); $arr =json_decode($data);

In addition to the http request, there is also an https request. Last time I did a one-click login on Renren.com, and its interface was the https url. Using the above function, an error was reported. If you also encounter such a problem, you can refer to the following methods to solve it.

https request example

The code is as follows
 代码如下 复制代码

function _https_curl_post($url, $vars) 

    foreach($vars as $key=>$value)
    {
        $fields_string .= $key.'='.$value.'&' ;
    } 
    $fields_string = substr($fields_string,0,(strlen($fields_string)-1)) ;
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  // this line makes it work under https
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POST, count($vars) );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);     
    $data = curl_exec($ch);        
    curl_close($ch);  
       
    if ($data)
    {
        return $data;
    }
    else
    {
        return false;
    }
}

Copy code
function _https_curl_post($url, $vars)
{
foreach($vars as $key=>$value)
{
$fields_string .= $key.'='.$value.'&' ;
}  
$fields_string = substr($fields_string,0,(strlen($fields_string)-1));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // this line makes it work under https
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, count($vars) );
Curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$data = curl_exec($ch); $data = curl_exec($ch);
curl_close($ch);
                                 
If ($data)
{
         return $data;
}  
else
{
         return false;
}  
}

http://www.bkjia.com/PHPjc/631544.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631544.html
TechArticle
http request includes two types, one is our ordinary http request login, the other is another https Request to log in, let me introduce to each student in detail how to use curl_init to implement htt...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!