The Curl of PHP

Source: Internet
Author: User
Tags array copy curl file upload ftp functions header access
  When I first contacted Curl, look at the documents, and the online search of various materials, the official (http://cn2.php.net/manual/en/intro.curl.php) explanation is that this is a libcurl library written by Daniel, PHP supports this extension library, allowing us to use various protocols (HTTP, HTTPS, FTP, Telnet, file ...) Access to a variety of servers, support for post, put, FTP or form based file upload, support cookies, agents, and so on.         as if it's still indefinitely, I think the simplest explanation is that curl is a toolset, supported by the Libcurl extension library, with functions that can simulate us to access certain addresses We manually in the browser address bar input http://www.baidu.com, to visit Baidu, Curl can replace this manual operation, the form of the program to achieve this process, this operation, the results of the process, is the browser to give us a Baidu search home.         Since Curl is a toolset, there are many functions to call, as can be imagined, enter the URL in the Address bar (may take some parameters), when the input is finished click Return, the equivalent of calling some functions, These functions are written on a script on one or several servers in Baidu, the function will have some effect after running, such as return the value, or not return a value, or print something, here presented a page to you, and curl can achieve more functions than simply show a page much more, These different functions are implemented through the CURL_SETOPT function. For example, when the SDK, others provide access to the form of the API, then curl on the way.        1.get implementation   Copy code <?php     $url = ' http://www.somesite.com ';     $data = Array (' username ' => ' Peter ', ' Password ' =>12345);     function Get ($url, $data = Array ()) {        $ch = Curl_init ();  //initialization of a curl resource type change Volume                 /* Set access Options * *         curl_setopt ($ch, Curlopt_f Ollowlocation, True); When  //is enabled, the server server returns location: Recursive return to server         curl_setopt in header ($ch, Curlopt_ Returntransfer, True);  //will get the data back instead of directly on the page output         curl_setopt ($ch, Curlopt_protocols, curlproto_http);  //set Access Address protocol type HTTP                 curl_setopt ($ch, Curlopt_connecttimeou T, 15); The timeout limit for  //access is 15s         $url = URL. '? '. Http_build_query ($data);           curl_setopt ($ch, Curlopt_url, $url);  //set the URL to be accessed           $result = curl_exec ($ch);  //to perform this visit, return a result        //...        :         & nbsp Do some operations on the correctness of the results         return $result;    }    //$result = Get (%url,$DATA);  //Call replication code       2.POST implementation   Copy code <?php     function post ($url, $data = Array ()) {&N Bsp       $ch = Curl_init ();                     CURL_SE Topt ($ch, Curlopt_post, true);  //is set to post delivery form         curl_setopt ($ch, Curlopt_postfields, $data);  //set post pass data         curl_setopt ($ch, Curlopt_url, $url);         curl_setopt ($ch, Curlopt_returntransfer, true);  //set data to return as file stream         curl_setopt ($ch, Curlopt_useragent, ");  //user agent null         curl_setopt ($ch, Curlopt_header, false);  //settings do not display header information                    $result = curl_exec ($ch);         $info = Curl_getinfo ($ch);  //obtain information on this access                    return $result; &nbsp  }   

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.