PHP uses curl_init to initiate an http request to simulate logon. There are two types of http requests: common http request logon and https request logon, next, I will introduce to you in detail how to use curl_init to implement htt http requests. one is our normal http request logon, the other is an https request for logon. next I will introduce the use of curl_init for http and https login respectively.
Note: to use the curl_init function, you must enable this php extension.
1. open php. ini and enable extension = php_curl.dll.
2. check php. which Directory is the extension_dir value of ini? check whether php_curl.dll exists. if not, download php_curl.dll and copy libeay32.dll and ssleay32.dll in the php directory to c:/windows/system32.
Initiate an http request
| The code is as follows: |
|
|
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); // 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: |
|
|
$ 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 http requests, there is also an https request. The last one-click login from the human network, the interface of which is the https url, using the above function, finally reports an error. If you 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;
}
} |
...