A few examples of Php curl

Source: Internet
Author: User
Tags urlencode

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.

Curl Libraries in PHP (Client URL library function)

curl_close-closing a Curl session

curl_copy_handle-Copy all the contents and parameters of a Curl connection resource

curl_errno-returns a numeric number that contains the current session error information

curl_error-returns a string containing the current session error message

Curl_exec-performing a Curl session

Curl_getinfo-gets the information for a Curl connection resource handle

curl_init-initialization of a curl session

curl_multi_add_handle-Adding a separate curl handle resource to a curl batch session

curl_multi_close-closing a batch handle resource

curl_multi_exec-parsing a Curl batch handle

curl_multi_getcontent-returns the text stream of the obtained output

Curl_multi_info_read-Gets the related transfer information for the currently resolved curl

curl_multi_init-initializing a curl batch handle resource

curl_multi_remove_handle-removing a handle resource from the Curl batch handle resource

Curl_multi_select-get all the sockets associated with the CURL extension, which can and be "selected"

curl_setopt_array-set session parameters as an array for a curl

curl_setopt-setting session parameters for a curl

curl_version-getting the version information about Curl

The Curl_init () function Initializes a curl session, and the only parameter to the Curl_init () function is optional, representing a URL address.

The function of the curl_exec () function is to perform a curl session, and the only argument is the handle returned by the Curl_init () function.

The function of the Curl_close () function is to close a curl session, and the only argument is the handle returned by the Curl_init () function.

Example one: basic example

﹤?php//Initializes a CURL object $curl = Curl_init ();//sets the Urlcurl_setopt ($curl, Curlopt_url, ' http://www.cmx8.cn ') you need to crawl; Set Headercurl_setopt ($curl, Curlopt_header, 1);//Set the curl parameter to require the result to be saved to a string or output to the screen. curl_setopt ($curl, Curlopt_returntransfer, 1);//Run Curl, request page $data = curl_exec ($curl);//close URL request curl_close ($curl);// Displays the data obtained var_dump ($DATA);? >

Example two: Post data

sendsms.php, which can accept two form fields, one is the phone number, and the other is the text message content.

﹤?php$phonenumber = ' 13812345678 '; $message = ' This message is generated by curl and php '; $curlPost = ' pnumber= '. UrlEncode ($phoneNumber). ' &message= '. UrlEncode ($message). ' &submit=send '; $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, ' http://www.lxvoip.com/sendSMS.php '); curl_ Setopt ($ch, Curlopt_header, 1); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_post, 1); Curl_ Setopt ($ch, Curlopt_postfields, $curlPost); $data = Curl_exec (); Curl_close ($ch);? ﹥

Example three: Using a proxy server

﹤?php $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, ' http://www.cmx8.cn '); curl_setopt ($ch, Curlopt_header, 1); Curl_ Setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_httpproxytunnel, 1); curl_setopt ($ch, Curlopt_proxy, ' proxy.lxvoip.com:1080 '); curl_setopt ($ch, curlopt_proxyuserpwd, ' User:password '); $data = Curl_exec (); Curl_close ($ CH);? ﹥

Example FOUR: Analog login

Curl Simulation Login Discuz program, suitable for DZ7.0, will username changed to your user name, Userpass changed to your password on it.

<?php    /**   * Curl  Analog Login  discuz  programs     *  Forum Login function    */      !extension_loaded (' curl ') that has not been implemented to enable verification codes )  && die (' the curl extension is not loaded. ');         $discuz _url =  ' http://www.lxvoip.com ';//Forum Address       $login _url =  $discuz _url . ' /logging.php?action=login ';//login page address      $get _url =  $discuz _url . ' /my.php?item=threads ';  //my posts         $post _fields = array ();     //The following two items do not need to modify      $post _fields[' Loginfield '] =  ' username ';      $post _fields[' loginsubmit '] =  ' true ';     //user name and password must be filled in      $post _fields[' username '] =  ' lxvoip ';     $post _fields[' pAssword '] =  ' 88888888 ';     //security Questions      $post _fields[' QuestionID '] = 0;     $post _fields[' answer '] =  ';     //@todo Verification Code      $post _fields[' seccodeverify '] =  ';        //get form formhash     $ch  = curl_init ($login _url);     curl_setopt ($ch,  curlopt_header, 0);     curl_setopt ($ch,  curlopt_returntransfer, 1);     $contents  = curl_exec ($ch);     curl_close ($ch);     preg_match ('/<input\s*type= "hidden" \s*name= "Formhash" \s *value= "(. *?)" \s*\/>/i ',  $contents,  $matches)     if (!empty ($matches))  {          $formhash  =  $matches [1];    } else  {        die (' Not found the forumhash. ');     }       //post data, get cookie    $ Cookie_file = dirname (__file__)  .  '/cookie.txt ';     //$cookie _FILE  = tempnam ('/tmp ');     $ch  = curl_init ($login _url);     curl_setopt ($ch,  curlopt_header, 0);     curl_setopt ($ch,  CURLOPT_ returntransfer, 1);     curl_setopt ($ch,  curlopt_post, 1);     curl_setopt ($ch, curlopt_postfields,  $post _fields);     curl_setopt ($ch,  CURLOPT_COOKIEJAR,  $cookie _file)     curl_exec ($ch);     Curl_close ($ch);        //with the cookie above gets the content of the page that needs to be logged in before it can be viewed       $ch  = curl_init ($get _url);     cuRl_setopt ($ch,  curlopt_header, 0);     curl_setopt ($ch,  curlopt_ returntransfer, 0);     curl_setopt ($ch, curlopt_cookiefile,  $cookie _file);      $contents  = curl_exec ($ch),     curl_close ($ch);        var_dump ($contents);    ?>


A few examples of Php curl

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.