Use CURL to submit POST Data

Source: Internet
Author: User
What is url: curl is a File Transfer tool that uses URL syntax in command line mode. Php Tutorial example :? Phpset_time_limit (0); @ date_default_timezone_set (AsiaShanghai); functioncurlrequest ($ url, $ postfield, $ proxy) {$ proxytrim ($ proxy); $ user_agentMozilla

What is url: curl is a File Transfer tool that uses URL syntax in command line mode. Php Tutorial example :? Php set_time_limit (0); @ date_default_timezone_set ('Asia/Shanghai'); function curlrequest ($ url, $ postfield, $ proxy = "") {$ proxy = trim ($ proxy); $ user_agent = "Mozilla/

What is url:

Curl is a File Transfer tool that uses the URL syntax to work in the command line mode.

Php Tutorial example:

Set_time_limit (0 );
@ Date_default_timezone_set ('Asia/Shanghai ');
Function curlrequest ($ url, $ postfield, $ proxy = ""){
$ Proxy = trim ($ proxy );
$ User_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )";
$ Ch = curl_init ();???? // Initialize the CURL handle
If (! Empty ($ proxy )){
Curl_setopt ($ ch, CURLOPT_PROXY, $ proxy); // sets the proxy Server
}
Curl_setopt ($ ch, CURLOPT_URL, $ url); // you can specify the request URL.
// Curl_setopt ($ ch, CURLOPT_FAILONERROR, 1); // The HTTP status code is displayed when it is enabled. The default behavior is to ignore the HTTP information whose number is less than or equal to 400.
// Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // When enabled, the "Location:" returned by the server is put in the header and recursively returned to the server.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // set to TRUE to convert the curl_exec () result into a string instead of directly outputting
Curl_setopt ($ ch, CURLOPT_POST, 1); // enable POST submission
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ postfield); // you can specify a string for POST submission.
// Curl_setopt ($ ch, CURLOPT_PORT, 80); // set the port
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 25); // timeout
Curl_setopt ($ ch, CURLOPT_USERAGENT, $ user_agent); // HTTP request User-Agent: Header
// Curl_setopt ($ ch, CURLOPT_HEADER, 1); // set TRUE to include header information in the output
// $ Fp = fopen ("example_homepage.txt", "w"); // output file
// Curl_setopt ($ ch, CURLOPT_FILE, $ fp); // set the location of the output file. The value is a resource type. The default value is STDOUT (browser ).
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array (
'Accept-Language: zh-cn ',
'Connection: Keep-alive ',
'Cache-Control: no-cache'
); // Set HTTP header information
$ Document = curl_exec ($ ch); // execute the predefined CURL
$ Info = curl_getinfo ($ ch); // obtain the returned information.
// Print_r ($ info );
If ($ info [http_code] = "405 "){
Echo "bad proxy {$ proxy} \ n ";?? // Proxy Error
Exit;
}
// Curl_close ($ ch );
Return $ document;
}
// Request URL
$ Url = "http://example.cn/getInfo.php ";
// Submit data in POST mode and can be viewed by HTTPWATCH.
$ Postfield = "userName = test & year = 2008 & passWord = 123456 & Submit = % CC % E1 % BD % BB ";
// Proxy Server
$ Proxy = '';
// Request
$ Str = curlrequest ($ url, $ postfield, $ proxy );
// Output result
Echo $ str;

Original article address: Use CURL to submit POST data. Thank you for sharing it with the original author.

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.