Post data _php tutorial with PHP curl

Source: Internet
Author: User
Tags ldap set time ssl certificate
Curl is a routing file tool that uses URL syntax to support FTP, FTPS, HTTP htpps SCP SFTP TFTP, TELNET DICT file, and LDAP. Curl supports SSL certificates, HTTP POST, http PUT, FTP uploads, Kerberos, HTT-based uploads, proxies, cookies, user + password proofs, file transfer recovery, HTTP proxy channels, and a number of other useful tricks.

It turns out that PHP does not extend this functionality by default, but it does, but it does not make it effective. 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 the php.ini file, find; extension= php_ Curl.dll line, remove the previous; number, save, restart the server.

Here are a few examples.

Short MMS Send

$xml _data =
   '. $pns. '
    '. $content. '
  '; $url = ' ';//Receive XML address $header = "content-type:text/xml";//define Content-type as XML $ch = Curl_init (); Initialize Curlcurl_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 header curl_setopt ($ch, Curlopt_post, 1);//set to POST mode curl_setopt ($ch, Curlopt_postfields, $xml _data);//post Data $response = curl_exec ($ch);//Receive return information if (Curl_errno ($ch)) {//error message Print Curl_error ($ch);} Curl_close ($ch); Turn off the curl link echo $response;//Display return information

Post Data Fetion interface

$username = 13800138000; $password = 123456; $sendto = 13912345678; $message = "Test one try it!" "; $curlPost = ' username= '. UrlEncode ($username). ' &password= '. UrlEncode ($password). ' &sendto= '. UrlEncode ($sendto). ' &message= '. UrlEncode ($message). "; $ch = Curl_init ();//Initialize Curlcurl_setopt ($ch, Curlopt_url, ' ');//Crawl specified page curl_setopt ($ch, Curlopt_header, 0);//Set Headercurl_setopt ($ch, Curlopt_returntransfer, 1);//require the result to be a string and output to the on-screen curl_setopt ($ch, curlopt _post, 1);//post Submission Method curl_setopt ($ch, Curlopt_postfields, $curlPost); $data = Curl_exec ($ch);//Run Curlcurl_close ($ch); Print_r ($data);//output result

Fetion interface mode: your mobile fetion login phone number, &password= your mobile fetion login password, &sendto= receive SMS Fetion friend phone number, &message= SMS content.

Summarize the use of the Curl method:

    1. Initialize Curl
    2. Use curl_setopt to set the destination URL, and other options
    3. Curl_exec, perform Curl
    4. After execution, turn off Curl
    5. The final step is to 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 multi-threaded task of PHP. Here's how to implement Multithreading:

 $url) {$conn [$k]=curl_init ($url); curl_setopt ($conn [$k], curlopt_timeout, $timeout);//Set time-out 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);//Do not HEADER, 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]); }//Prevent death cycle consumes the CPU this paragraph is based on the online notation do {$MRC = Curl_multi_exec ($MH, $active);//When 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 a request is paused, Active=trueif (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]); Release Resource}curl_multi_close ($MH); $endtime = Getmicrotime (); $diff _time = $endtime-$startime; return Array (' diff_time ' =  > $diff _time, ' return ' and $res, ' header ' and ' $header ');    }//Calculates the current time function getmicrotime () {list ($usec, $sec) = Explode ("", Microtime ()); return (float) $usec + (float) $sec);} Test, curl three urls $array = Array ("", "", ""); $data = Curl_ HTTP ($array, ' 10 ');//Call Var_dump ($DATA);//Output?>

Because $active to wait for all the URL data to be completed before it becomes false, so here to use the Curl_multi_exec return value to determine whether there is data, when there is data when the call Curl_multi_exec, There is no data to enter the Select phase, and new data can be woken up and executed. The advantage here is that the CPU is useless.

This multi-threaded writing step:

    1. Call Curl_multi_init
    2. Loop call Curl_multi_add_handle, this step needs to be noted that the second parameter of Curl_multi_add_handle is a child handle from Curl_init.
    3. Continue calling Curl_multi_exec
    4. Loop call Curl_multi_getcontent as needed to get results
    5. Call Curl_multi_remove_handle, and call curl_close for each word handle
    6. Call Curl_multi_close true techarticle Curl is a routing file tool that uses URL syntax to support FTP, FTPS, HTTP htpps SCP SFTP TFTP, TELNET DICT file, and LDAP. Curl supports SSL certificate, HTTP POST, http PUT, FTP upload, ker ...

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