There are two types of http requests: common http request logon and https request logon, next I will give you a detailed introduction of using curl_init to implement http and h
There are two types of http requests: common http request logon and https request logon, next I will give you a detailed description of using curl_init to achieve http and https login.
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.
The code for initiating an http request 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;
- }
The code is as follows:
- $ Params = array ();
- $ Params ['id'] = 1
- $ Params ['web _ name'] = 'good script ';
- $ Params ['web _ url'] = 'http: // www.phpfensi.com /';
- $ 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 time I log on to Renren, the interface is the https url. Using the above function, an error is reported, if you encounter such a problem, you can refer to the following methods to solve it.
Https request example 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 );
- Curl_close ($ ch );
- If ($ data)
- {
- Return $ data;
- }
- Else
- {
- Return false;
- }
- }