Post Data using PHP Curl

Source: Internet
Author: User
Tags foreach curl current time diff ftp http post urlencode

Curl is a transfer file tool using URL syntax that supports FTP, FTPS, HTTP htpps SCP SFTP TFTP TELNET DICT file and LDAP. Curl supports SSL certificates, HTTP POST, HTTP put, FTP uploads, Kerberos, upload based on htt format, proxy, cookie, user + password proof, File transfer recovery, HTTP proxy channel, and a number of other useful tips.

The original PHP default does not carry out this feature expansion, but still some, but did not let it take effect. Open the PHP installation directory, search the following three files Ssleay32.dll, Libeay32.dll and Php_curl.dll, one by one copy to the system directory under the System32 folder, modify php.ini files, find; extension= php_ Curl.dll the line, remove the front number, save, reboot the server.

Let me give you a few examples.

Short MMS Send

$xml _data = '

'. $pns. '


$url = ' ';//Receive XML address

$header = "Content-type:text/xml";//define Content-type as XML

$ch = Curl_init (); Initialize Curl

curl_setopt ($ch, Curlopt_url, $url);/Set link

curl_setopt ($ch, Curlopt_returntransfer, 1);/Set whether to return information

curl_setopt ($ch, Curlopt_httpheader, $header);//Set HTTP headers

curl_setopt ($ch, Curlopt_post, 1);//Set to POST method

curl_setopt ($ch, Curlopt_postfields, $xml _data);//post data

$response = curl_exec ($ch);//Receive return information

if (Curl_errno ($ch)) {//error message is displayed

Print Curl_error ($ch);


Curl_close ($ch); Close Curl Link

echo $response//Display return information

Post Data Flying Letter interface

$username = 13800138000;

$password = 123456;

$sendto = 13912345678;

$message = "Test a try!";

$curlPost = ' username= '. UrlEncode ($username). ' &

Password= '. UrlEncode ($password). ' &

Sendto= '. UrlEncode ($sendto). ' &

Message= '. UrlEncode ($message). ';

$ch = Curl_init ();//Initialize Curl

curl_setopt ($ch, Curlopt_url, ' '); crawl the specified page

curl_setopt ($ch, Curlopt_header, 0);/Set HEADER

curl_setopt ($ch, Curlopt_returntransfer, 1);/request result is string and output to screen

curl_setopt ($ch, Curlopt_post, 1);//post Submission method

curl_setopt ($ch, Curlopt_postfields, $curlPost);

$data = curl_exec ($ch);//Run Curl

Curl_close ($ch);

Print_r ($data);//output result

Flying Letters Interface mode: Http:// your mobile letter login mobile phone number, &password= your mobile login password, &sendto= receive text message of the Flying friend mobile phone number, &message= SMS content.

To summarize the use of the Curl method:

Initialize Curl

Use curl_setopt to set the target URL, and other options

Curl_exec, Execute Curl

After execution, close curl

The final step is the output

Cerl Multithreading

Curl is generally used to crawl Web pages, the second is GET or post data, the third application is to implement the multithreaded task of PHP. Here's how to implement Multithreading:


Curl Multi-threaded crawl



* Curl Multithreading


* @param array $array parallel URLs

* @param int $timeout timeout

* @return Array


function Curl_http ($array, $timeout) {

$res = Array ();

$MH = Curl_multi_init ();//Create Multiple Curl handles

$startime = Getmicrotime ();

foreach ($array as $k => $url) {

$conn [$k]=curl_init ($url);

curl_setopt ($conn [$k], curlopt_timeout, $timeout);/Set timeout time

curl_setopt ($conn [$k], Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0);

curl_setopt ($conn [$k], Curlopt_maxredirs, 7);//http Orientation level

curl_setopt ($conn [$k], Curlopt_header, 0);/no HEADER here, add block efficiency

curl_setopt ($conn [$k], curlopt_followlocation, 1); 302 redirect

curl_setopt ($conn [$k],curlopt_returntransfer,1);

Curl_multi_add_handle ($MH, $conn [$k]);


Preventing dead loops from dying the CPU this paragraph is based on the online wording

do {

$MRC = Curl_multi_exec ($MH, $active);//When there is no data, active=true

while ($MRC = = Curlm_call_multi_perform);////When data is being accepted

while ($active and $MRC = = CURLM_OK) {//when there is no data or when the request is paused, active=true

if (Curl_multi_select ($MH)!=-1) {

do {

$MRC = Curl_multi_exec ($MH, $active);

while ($MRC = = Curlm_call_multi_perform);



foreach ($array as $k => $url) {

Curl_error ($conn [$k]);

$res [$k]=curl_multi_getcontent ($conn [$k]);//Get return information

$header [$k]=curl_getinfo ($conn [$k]);//Return header information

Curl_close ($conn [$k]);//close handle

Curl_multi_remove_handle ($MH, $conn [$k]); Releasing resources


Curl_multi_close ($MH);

$endtime = Getmicrotime ();

$diff _time = $endtime-$startime;

Return Array (' Diff_time ' => $diff _time,

' Return ' => $res,

' Header ' => $header



Calculate Current time

function Getmicrotime () {

List ($usec, $sec) = Explode ("", Microtime ());

Return ((float) $usec + (float) $sec);


Test it, curl three URLs

$array = Array (





$data = Curl_http ($array, ' 10 ');

Var_dump ($data);//Output


Because $active to wait for all the URL data to become false, so here used the return value of curl_multi_exec to judge whether there is data, when there is data, call Curl_multi_exec, There is no data at the moment, and the new data can be awakened to continue execution as soon as it enters the select phase. The advantage here is that the unnecessary consumption of the CPU is gone.

This multithreaded approach to writing:

Call Curl_multi_init

Loop call Curl_multi_add_handle, this step is to note that the second parameter of Curl_multi_add_handle is the handle of the son from Curl_init.

Continue calling Curl_multi_exec

Loop call Curl_multi_getcontent to get results as needed

Call Curl_multi_remove_handle, and call curl_close for each word handle

Call Curl_multi_close

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: 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.