How to use curl to implement get and post requests in PHP

Source: Internet
Author: User
Tags how to use curl
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

   Initialize       $curl = Curl_init ();      Set crawl URL      curl_setopt ($curl, Curlopt_url, ' http://www.baidu.com ');       Set the header file information as the data stream output      curl_setopt ($curl, Curlopt_header, 1);       The information obtained is returned in the form of a file stream, rather than as a direct output.       curl_setopt ($curl, Curlopt_returntransfer, 1);       Execute command      $data = curl_exec ($curl);      Close URL request      curl_close ($curl);      Display the obtained data      Print_r ($data);

②:post method implements the

//Initialize $curl = Curl_init ();       Set crawl URL curl_setopt ($curl, Curlopt_url, ' http://www.baidu.com ');       Set the header file information as the data stream output curl_setopt ($curl, Curlopt_header, 1);       The information obtained is returned in the form of a file stream, rather than as a direct output.       curl_setopt ($curl, Curlopt_returntransfer, 1);      Set POST mode to submit curl_setopt ($curl, Curlopt_post, 1);      Set the post data $post _data = Array ("username" = "coder", "Password" and "12345");      curl_setopt ($curl, Curlopt_postfields, $post _data);      Execute command $data = curl_exec ($curl);      Close URL request curl_close ($curl); Displays the obtained data 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

  Parameter 1: URL to access, parameters: Post data (not filled is get), Parameter 3: $cookies submitted, Parameter 4: Return $cookies 2 function curl_request ($url, $post = ", $cookie ="           , $returnCookie =0) {$curl = Curl_init ();           curl_setopt ($curl, Curlopt_url, $url); curl_setopt ($curl, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1;          trident/6.0) ');           curl_setopt ($curl, curlopt_followlocation, 1);           curl_setopt ($curl, Curlopt_autoreferer, 1);           curl_setopt ($curl, Curlopt_referer, "Http://XXX");              if ($post) {curl_setopt ($curl, Curlopt_post, 1);          curl_setopt ($curl, Curlopt_postfields, Http_build_query ($post));          } if ($cookie) {curl_setopt ($curl, Curlopt_cookie, $cookie);          } curl_setopt ($curl, Curlopt_header, $returnCookie);          curl_setopt ($curl, Curlopt_timeout, 10);          curl_setopt ($curl, Curlopt_returntransfer, 1);          $data = curl_exec ($curl);         if (Curl_errno ($curl)) {     Return Curl_error ($curl);          } curl_close ($curl);              if ($returnCookie) {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 '] = $body;          return $info;          }else{return $data; }  }

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.