POST data using PHPCURL _ PHP Tutorial

Source: Internet
Author: User
Use the POST data of PHPCURL. Curl is a file transfer tool using URL syntax. it supports FTP, FTPS, HTTPHTPPSSCPSFTPTFTPTELNETDICTFILE, and LDAP. Curl supports SSL certificates, HTTPPOST, HTTPPUT, and FTP uploads. ker curl is a FILE transfer tool using URL syntax. it supports FTP, FTPS, http htpps scp sftp tftp telnet dict file and LDAP. Curl supports SSL certificates, http post, http put, and FTP uploads, kerberos, HTT-based Upload, proxy, cookie, user + password proof, file transfer recovery, http proxy channel and a large number of other useful techniques.

Php does not extend this function by default, but it still does not take effect. Open the PHP installation directory, search for the following three files: ssleay32.dll, libeay32.dll, and php_curl.dll, copy them to the system32 folder in the system directory one by one, and modify php. ini file, find; extension = php_curl.dll line, remove the previous; number, save, restart the server.

The following are examples.

Short MMS sending
$ Xml_data ='
 
 
   
  
   
  
   
  
   
  
  
   
'. $ Pns .'
  
  
   '. $ Content .'
  
 '; $ Url = 'http: // www.bkjia.com/service/tasksubmit'{//receiving xmladdress Authorization header = "Content-type: text/xml"; // Define content-type as xml $ ch = curl_init (); // initialize curlcurl_setopt ($ ch, CURLOPT_URL, $ url); // Set the link curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // Set whether to return information curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); // Set the HTTP header curl_setopt ($ ch, CURLOPT_POST, 1); // Set it to the POST method curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ xml_data ); // POST data $ response = curl_exec ($ ch); // receives the returned information if (curl_errno ($ ch )) {// print curl_error ($ ch);} curl_close ($ ch); // Close the curl link echo $ response; // display the returned information.
POST data Feixin interface
$ Username = 13800138000; $ password = 123456; $ sendto = 13912345678; $ message = "try it out! "; $ CurlPost = 'username = '. urlencode ($ username ). '& password = '. urlencode ($ password ). '& sendto = '. urlencode ($ sendto ). '& message = '. urlencode ($ message ). ''; $ ch = curl_init (); // initialize curlcurl_setopt ($ ch, CURLOPT_URL, 'http: // sms. api. bz/fetion. php '); // capture the specified webpage curl_setopt ($ ch, CURLOPT_HEADER, 0); // Set headercurl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // the result must be a string and be output to the screen. curl_setopt ($ ch, CURLOPT_POST, 1); // The post submission method curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost ); $ data = curl_exec ($ ch); // run curlcurl_close ($ ch); print_r ($ data); // output the result

Fetion interface mode: http://sms.api.bz/fetion.php? Username = your mobile phone number, & password = your mobile phone password, & sendto = the mobile phone number of your friends who receive text messages, & message = text message content.

To sum up the curl method:

  1. Initialize curl
  2. Use curl_setopt to set the target url and other options
  3. Curl_exec: execute curl
  4. Disable curl
  5. The last step is output.
CERL multithreading

Curl is generally used to capture web pages, the second is get or post data, and the third is to implement PHP multi-threaded tasks. Multithreading is implemented as follows:

 $ Url) {$ conn [$ k] = curl_init ($ url); curl_setopt ($ conn [$ k], CURLOPT_TIMEOUT, $ timeout ); // Set the timeout value 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 targeting 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], CURLO PT_RETURNTRANSFER, 1); curl_multi_add_handle ($ mh, $ conn [$ k]);} // prevent endless loop cpu consumption. do {$ mrc = curl_multi_exec ($ mh, $ active) according to the online statement; // when no data exists, active = true} while ($ mrc = CURLM_CALL_MULTI_PERFORM); // while receiving data ($ active and $ mrc = CURLM_ OK) {// when there is no data or the 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 the returned information $ header [$ k] = curl_getinfo ($ conn [$ k]); // return header information curl_close ($ conn [$ k]); // Close the language handle curl_multi_remove_handle ($ mh, $ conn [$ k]); // release resources} curl_multi_close ($ mh); $ endtime = getmicrotime (); $ diff_time = $ endtime-$ startime; return array ('Diff _ time' => $ diff_time, 'Return '=> $ res, 'header' => $ header );} // calculate the current time function getmicrotime () {list ($ usec, $ sec) = explode ("", microtime (); return (float) $ usec + (float) $ sec);} // test the curl url. $ array = array (" http://www.weibo.com/ "," http://www.renren.com/ "," http://www.qq.com/ "); $ Data = Curl_http ($ array, '10'); // call var_dump ($ data); // Output?>

Because $ active changes to false only after all url data is accepted, the returned value of curl_multi_exec is used to determine whether there is any data. when there is data, curl_multi_exec is called continuously, if no data is available for the moment, the select phase is started, and the new data can be awakened to continue execution. The advantage here is that there is no unnecessary CPU consumption.

The multi-thread writing steps:

  1. Call curl_multi_init
  2. Call curl_multi_add_handle cyclically. Note that the second parameter of curl_multi_add_handle is the sub-handle generated by curl_init.
  3. Continuous Call of curl_multi_exec
  4. Call curl_multi_getcontent cyclically to obtain the result as needed.
  5. Call curl_multi_remove_handle and call curl_close for each handle.
  6. Call curl_multi_close

Http://www.bkjia.com/PHPjc/752459.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752459.htmlTechArticlecurl is a transfer FILE tool that uses URL syntax and supports 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: 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.