CURL application is widely used. This article describes two methods for CURL to simulate POST submission. They all return the json string format. CURL application is widely used. This article describes two methods for CURL to simulate POST submission. They all return the json string format.
Script ec (2); script
Method 1 (json string format is returned ):
| The Code is as follows: |
|
/**
* Curl version
* Usage:
* $ Post_string = "app = request & version = beta ";
* Request_by_curl ('HTTP: // facebook.cn/restserver.php', then post_string );
*/
Function actionPost ($ url, $ data) {// simulate the data submission function
$ Curl = curl_init (); // start a CURL session
Curl_setopt ($ curl, CURLOPT_URL, $ url); // address to be accessed
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // check the certificate Source
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate
Curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['HTTP _ USER_AGENT ']); // simulate the browser used by the user
Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect
Curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1); // automatically sets Referer
Curl_setopt ($ curl, CURLOPT_POST, 1); // send a regular Post request
Curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data); // data packet submitted by Post
Curl_setopt ($ curl, CURLOPT_COOKIEFILE, 'cookie.txt '); // read the Cookie information stored above
Curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // sets the timeout limit to prevent endless loops.
Curl_setopt ($ curl, CURLOPT_HEADER, 0); // display the returned Header content.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information is returned as a file stream.
$ TmpInfo = curl_exec ($ curl); // perform the operation
If (curl_errno ($ curl )){
Echo 'errno'. curl_error ($ curl );
}
Curl_close ($ curl); // key CURL session
Return $ tmpInfo; // return data
}
|
Method 2 (json string format is returned ):
| The Code is as follows: |
|
Function actionCurl ($ remote_server, $ post_string ){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_URL, $ remote_server );
// To support cookies
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, 'cookie.txt ');
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_string );
$ Result = curl_exec ($ ch );
Return $ result;
} |