Example of Two Methods for simulating POST submission using CURL-PHP source code

Source: Internet
Author: User
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.