Use php curl post Data

Source: Internet
Author: User

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 = '<? Xml version = "1.0" encoding = "UTF-8" standalone = "yes"?> <TaskDataTransfer4EReq xmlns = "http://www.aspirehld.com/iecp/TaskDataTransfer4EReq"> <eid> </eid> <username> </username> <password> </password> <src> </src> <destmsisdn>'. $ pns. '</destmsisdn> <content type = "sms"> <title> '. $ content. '</title> </content> </TaskDataTransfer4EReq>'; $ 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 the information curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); // set the HTTP header curl_setopt ($ ch, CURLOPT_POST, 1 ); // set the POST method to curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ xml_data); // POST Data $ response = curl_exec ($ ch ); // receive returned information if (curl_errno ($ ch) {// error message 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:

<? Php/* curl multi-thread crawling * // *** curl multithreading ** @ param array $ array parallel URL * @ param int $ timeout time * @ return array */function Curl_http ($ array, $ timeout) {$ res = array (); $ mh = curl_multi_init (); // create multiple curl scripts $ startime = getmicrotime (); foreach ($ array as $ k => $ 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 (compa Tible; MSIE 5.01; Windows NT 5.0) '); curl_setopt ($ conn [$ k], CURLOPT_MAXREDIRS, 7); // HTTp Targeting level curl_setopt ($ conn [$ k], CURLOPT_HEADER, 0); // do not use header here. Add the 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 endless loop consumption of cpu This section is based on the online writing do {$ mrc = curl_multi_exec ($ mh, $ active); // when no data is available, active = true} while ($ Mrc = CURLM_CALL_MULTI_PERFORM); // while receiving data ($ active and $ mrc = CURLM_ OK) {// when there is no data or when 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-$ sta Rtime; 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, curl three URLs $ 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

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.