Php uses curl to send post data

Source: Internet
Author: User
As a third-party developer, you often need to call the platform interface. For remote calls, you need to use curl. the essence is that the method and parameters used are sent to the platform server in httppost mode. Simple Example: $ url; platform service address for calling the interface $ post_stringarray (AB); $ chcurl_init ();

As a third-party developer, you often need to call the platform interface. For remote calls, you need to use curl. The essence is to send the call method and parameters to the platform server through http post. Simple Example: $ url = 'HTTP: // '; // The Platform service address for calling the interface $ post_string = array ('A' =' B '); $ ch = curl_init ();

As a third-party developer, you often need to call the platform interface. For remote calls, you need to use curl. The essence is to send the call method and parameters to the platform server through http post.

Simple Example:

$ Url = 'HTTP: // '; // The Platform service address for calling the interface

$ Post_string = array ('A' => 'B ');

$ Ch = curl_init ();

Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_string );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 10 );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 30 );
$ Result = curl_exec ($ ch );
Curl_close ($ ch );

In fact, the setting of CURLOPT_POSTFIELDS can also directly send arrays, but the consequence is that the request time is greatly increased, which takes about 2 seconds, for the same operation, the socket operation is normal in milliseconds.

If you use curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_string );
Changed to: curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ post_data ));

The execution time returns to normal.

PS:

Http_build_query-Generate the request string after URL-encode

Description

StringHttp_build_query(Array $ formdata [, string $ numeric_prefix])

Generate a URL-encode request string using the given correlated (or subscript) array. ParametersFormdataIt can be an array or an object containing attributes. OneFormdataAn array can be a simple one-dimensional structure or an array composed of arrays (which can contain other arrays in turn ). If the numeric subscript is used in the basic arrayNumeric_prefixParameter. The value of this parameter is used as the prefix of the numeric subscript element in the base array. This is to allow PHP or other CGI programs to obtain valid variable names when decoding data later.

Note:

Use arg_separator.output to separate parameters.

Example #1 http_build_query () Example

$ Data = array ('foo' => 'bar ',
'Baz' => 'boom ',
'Cow' => 'milk ',
'Php' => 'hypertext processor'

);

Echo http_build_query ($ data); // foo = bar & baz = boom & cow = milk & php = hypertext + processor
?>

Example #2 http_build_query () use the following element of a number

$ Data = array ('foo', 'bar', 'baz', 'boom ', 'cow' => 'milk ', 'php' => 'hypertext processor ');

Echo http_build_query ($ data );


Echo http_build_query ($ data, 'myvar _');

?>

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.