Use curl in PHP for Get and POST requests

Source: Internet
Author: User

CURL is a tool that uses URL syntax to transfer files and data, and supports many protocols, such as HTTP, FTP, Telnet, and so on. Best of all, PHP also supports the CURL library. Using the PHP Curl Library, you can easily and effectively grab pages. You just need to run a script, then analyze the page you crawled, and then you can get the data you want in a program. Whether you want to take part of the data from a link, or take an XML file and import it into the database, the fear is simply to get the Web content, CURL is a powerful PHP library.

Basic steps for PHP to build a curl request

①: Initialization

Curl_init ()

②: Setting properties

curl_setopt (). There is a long string of curl parameters that can be set to specify the details of the URL request.

③: Execute and get results

Curl_exec ()

④: Release handle

Curl_close ()

Curl implements get and post

①:get Method implementation

1  //Initialize 2     $curl = Curl_init (); 3     //Set crawl URL 4     curl_setopt ($curl, Curlopt_url, ' http://www.baidu.com ') ; 5     //Set header file information as data stream output 6     curl_setopt ($curl, Curlopt_header, 1); 7     //Set the information obtained is returned as a file stream, rather than as a direct output. 8     curl_setopt ($curl, Curlopt_returntransfer, 1); 9     //execution order     $data = curl_exec ($curl);     Close URL request     curl_close ($curl);     //Show data obtained from     Print_r ($data);

②:post Method implementation

1//Initialize 2     $curl = Curl_init (); 3     //Set fetch URL 4     curl_setopt ($curl, Curlopt_url, ' http://www.baidu.com '); 5     //Set header file information as data stream output 6     curl_setopt ($curl, Curlopt_header, 1); 7     //Set the information obtained is returned as a file stream, rather than as a direct output. 8     curl_setopt ($curl, Curlopt_returntransfer, 1); 9     //Set POST method submitted by     curl_setopt ($curl, Curlopt_post, 1); One     //Set post data     $post _data = Array (         "username" = "coder",         "password" and "12345" 15         );     curl_setopt ($curl, Curlopt_postfields, $post _data); +     //execute order     $data = curl_exec ($curl); /     /Close URL request     curl_close ($curl);     //Display data obtained in     Print_r ($data);

③: If the data is obtained in JSON format, use the Json_decode function to interpret the array.

$output _array = Json_decode ($data, true); If the second argument is true, it is converted to the form of an array. If you do not fill in the form of an object

If you use Json_decode ($data) parsing, you will get data of type object.

a function of my own encapsulation
 1//Parameter 1: URL to access, parameter 2:post data (not filled in is get), Parameter 3: $cookies submitted, Parameter 4: Returns $cookies 2 function Curl_request ($url, $post = ", $cookie = ", $returnCookie =0) {3 $curl = Curl_init (); 4 curl_setopt ($curl, Curlopt_url, $url); 5 curl_setop T ($curl, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; trident/6.0) '); 6 curl_setopt ($curl, curlopt_followlocation, 1); 7 curl_setopt ($curl, Curlopt_autoreferer, 1); 8 curl_setopt ($curl, Curlopt_referer, "Http://XXX"); 9 if ($post) {curl_setopt ($curl, Curlopt_post, 1); curl_setopt ($curl, Curlopt_postfiel DS, Http_build_query ($post)),}13 if ($cookie) {curl_setopt ($curl, Curlopt_cookie, $cookie );}16 curl_setopt ($curl, Curlopt_header, $returnCookie); curl_setopt ($curl, Curlopt_timeout, curl_setopt ($curl, Curlopt_returntransfer, 1), $data = Curl_exec ($curl), if (Curl_errno ($curL) {Curl_error return ($curl),}23 curl_close ($curl), if ($returnCookie) {25 List ($header, $body) = Explode ("\r\n\r\n", $data, 2); Preg_match_all ("/set\-cookie: ([^;] *);/", $header, $matches), $info [' cookie '] = substr ($matches [1][0], 1); $info [' content '] = $b ody;29 return $info;}else{31 return $data; 32}33}

Use curl in PHP for Get and post requests

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.