Use of curl in PHP

Source: Internet
Author: User

CURL function

  • curl_close-closing a Curl session
  • curl_copy_handle-copy a curl handle and all of its options
  • curl_errno-returns the last error number
  • curl_error-returns a string that protects the most recent error of the current session
  • Curl_escape-a given string using URL encoding
  • Curl_exec-performing a Curl session
  • curl_file_create-Creating a Curlfile Object
  • 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 to a curl batch session
  • curl_multi_close-closing a set of curl handles
  • curl_multi_exec-A child connection that runs the current CURL handle
  • curl_multi_getcontent-if Curlopt_returntransfer is set, 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-returns a new Curl batch handle
  • curl_multi_remove_handle-removing a handle resource from the Curl batch handle resource
  • curl_multi_select-waiting for active connections in all Curl batches
  • curl_multi_setopt-setting an option for CURL parallel processing
  • Curl_multi_strerror-return string describing error code
  • Curl_pause-pause and unpause a connection
  • Curl_reset-reset all options of a libcurl session handle
  • curl_setopt_array-Bulk setup options for Curl transfer sessions
  • curl_setopt-Setting a Curl Transfer option
  • Curl_share_close-close a CURL share handle
  • Curl_share_init-initialize a CURL share handle
  • Curl_share_setopt-set an option for a CURL share handle.
  • Curl_strerror-return string describing the given error code
  • curl_unescape-decoding a given URL-encoded string
  • curl_version-Getting Curl Version information

1. A simple example

<?php//Create a new curl resource $ch = Curl_init ();//Set URL and corresponding options curl_setopt ($ch, Curlopt_url, "http://www.example.com/"); curl_ Setopt ($ch, Curlopt_header, false);//Fetch the URL and pass it to the browser curl_exec ($ch);//Close the Curl resource and release the system Resource Curl_close ($CH); >

2. Setting of head information

$header [] = ' accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 '; $header [] = ' accept-encoding: gzip, deflate '; $header [] = ' accept-language:en '; $header [] = ' cache-control:max-age=0 '; $header [] = ' Connection: Keep-alive '; $header [] = ' cookie:phpsessid=jkuflj1jgoo7vvh5fcae3md262; Qtrans_cookie_test=1 '; $header [] = ' dnt:1 '; $header [] = ' client-ip:58.68.44.61 '; $header [] = ' x-forwarded-for : 58.68.44.61 ';//$header [] = ' Host:www.baidu.com '; $header [] = ' If-modified-since:tue, Sep 03:07:27 GMT '; $header [ ] = ' user-agent:mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) gecko/20100101 firefox/40.0 ';

3. Setting of setopt information

$cookie = Tempnam ('/data/www/tmp/', ' seek '); curl_setopt ($ch, curlopt_header,0); curl_setopt ($ch, Curlopt_ssl_ Verifyhost, 0); curl_setopt ($ch, Curlopt_ssl_verifypeer, 0); curl_setopt ($ch, curlopt_followlocation,1); curl_setopt ($ch, curlopt_maxredirs); curl_setopt ($ch, Curlopt_ autoreferer,1); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_httpheader, $header); curl_ Setopt ($ch, Curlopt_cookiejar, $cookie); if ($type = = ' POST ' &&!empty ($data)) {//post && is not empty $data curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_postfields, Http_build_query ($data));} curl_setopt ($ch, Curlopt_connecttimeout), curl_setopt ($ch, curlopt_encoding, ' gzip,deflate '); curl_setopt ($ch, CURLOPT_TIMEOUT,120); $content = Curl_exec ($ch);                                                                                                        Curl_close ($ch);

4. File Save

$TP = @fopen ($path, ' a '); Fwrite ($TP, $content); fclose ($TP);

5. Login FTP

<?php    $username = ' username ';    $password = ' password ';    $url = ' example.com ';    $ftp _server = "ftp://". $username. ":" . $password. "@" . $url;       echo "Starting curl.\n";    $ch = Curl_init ();    echo "Set CURL url.\n";       Curl FTP    curl_setopt ($ch, Curlopt_url, $ftp _server);       For debugging    //curl_setopt ($ch, Curlopt_verbose, TRUE);       SSL Settings    curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);    curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE);    curl_setopt ($ch, Curlopt_ftp_ssl, curlftpssl_try);       List FTP files and directories    curl_setopt ($ch, curlopt_ftplistonly, TRUE);       Output to Curl_exec    curl_setopt ($ch, Curlopt_returntransfer, 1);    echo "Executing curl.\n";    $output = curl_exec ($ch);    Curl_close ($ch);    echo "Closing curl.\n";    Echo $output. "\ n";   $files = explode ("\ n", $output);   Print_r ($files);? >

5, the use of agents

<?php $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, ' http://www.baidu.com '); 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);? >

Some parameter descriptions

1. Enable debugging

curl_setopt ($ch, Curlopt_verbose, TRUE);

2. SSL Settings

curl_setopt ($ch, Curlopt_ssl_verifypeer, false); curl_setopt ($ch, Curlopt_ssl_verifyhost, false); Orcurl_setopt ($ch, Curlopt_verbose, ' 1 '); curl_setopt ($ch, Curlopt_ssl_verifyhost, ' 2 '); curl_setopt ($ch, Curlopt_ssl_verifypeer, ' 1 '); curl_setopt ($ch, Curlopt_cainfo,  

3. Session

4. Parameter explanation

Curlopt_autoreferer  when based on location: redirection, the Referer in the header is automatically set: Information curlopt_followlocation is enabled when the server server returns the "location:" Recursively returns to the server in the header, using Curlopt_maxredirs to limit the number of recursive returns Curlopt_header when enabled, the header file information is output as a stream (close when saving the picture) Curlopt_nobody When enabled, the body part of the HTML is not output Curlopt_post enabled, a regular POST request is sent with the type: application/x-www-form-urlencoded, just as the form commits curlopt_ Returntransfer returns the information obtained by CURL_EXEC () as a file stream instead of the direct output curlopt_timeout  setting the maximum number of seconds that curl is allowed to perform Curlopt_cookie setting the HTTP request " Cookie: "section of the content. Multiple cookies are separated by semicolons, with a semicolon followed by a space (for example, "fruit=apple; Colour=red ") curlopt_cookiefile contains the file name of the cookie data, the format of the cookie file can be Netscape format, or just plain HTTP header information is stored in the file Curlopt_cookiejar The value of "accept-encoding:" in the HTTP request header for the file that holds the cookie information after the connection ends. curlopt_encoding The supported encodings are "identity", "deflate" and "gzip". If an empty string "", the request header sends all supported encoding types Curlopt_postfields  All data uses the "POST" action in the HTTP protocol to send Curlopt_httpheader an array to set the HTTP header field. The array is set using the following form: Array (' Content-type:text/plain ', ' content-length:100 ')

Reference Address: http://php.net/manual/zh/function.curl-setopt.php

Use of curl in PHP

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.