Example of PHP curl_init function usage

Source: Internet
Author: User
    1. Initialize a Curl Object
    2. $curl = Curl_init ();
    3. Set the URL you need to crawl
    4. curl_setopt ($curl, Curlopt_url, ' http://bbs.it-home.org ');
    5. Set Header
    6. curl_setopt ($curl, Curlopt_header, 1);
    7. Sets the curl parameter, which requires the result to be saved to a string or output to the screen.
    8. curl_setopt ($curl, Curlopt_returntransfer, 1);
    9. Run Curl, request a Web page
    10. $data = curl_exec ($curl);
    11. Close URL Request
    12. Curl_close ($curl);
    13. Show the data obtained
    14. Var_dump ($data);
    15. ?>
Copy Code

Example 2:post data sendsms.php, which can accept two form fields, one is the phone number, and the other is the text message content. Post data

    1. $phonenumber = ' 13812345678 ';
    2. $message = ' This message is generated by curl and PHP ';
    3. $curlpost = ' pnumber= '. UrlEncode ($phonenumber). ' &message= '. UrlEncode ($message). ' &submit=send ';
    4. $ch = Curl_init ();
    5. curl_setopt ($ch, Curlopt_url, ' http://www.lxvoip.com/sendsms.php ');
    6. curl_setopt ($ch, Curlopt_header, 1);
    7. curl_setopt ($ch, Curlopt_returntransfer, 1);
    8. curl_setopt ($ch, Curlopt_post, 1);
    9. curl_setopt ($ch, Curlopt_postfields, $curlpost);
    10. $data = Curl_exec ();
    11. Curl_close ($ch);
    12. ?>
Copy Code

Example 3: Using Proxy server with Proxy server

    1. $ch = Curl_init ();
    2. curl_setopt ($ch, Curlopt_url, ' http://bbs.it-home.org ');
    3. curl_setopt ($ch, Curlopt_header, 1);
    4. curl_setopt ($ch, Curlopt_returntransfer, 1);
    5. curl_setopt ($ch, Curlopt_httpproxytunnel, 1);
    6. curl_setopt ($ch, Curlopt_proxy, ' proxy.lxvoip.com:1080 ');
    7. curl_setopt ($ch, curlopt_proxyuserpwd, ' User:password ');
    8. $data = Curl_exec ();
    9. Curl_close ($ch);
    10. ?>
Copy Code

Example 4: Simulation Login Curl Simulation Login Discuz program, suitable for dz7.0, will username changed to your user name, Userpass changed to your password on it. Curl Simulation Login Discuz Program

!extension_loaded (' curl ') && die (' The curl extension are 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 be modified $post _fields[' loginfield '] = ' username '; $post _fields[' loginsubmit ') = ' true '; User name and password must be filled in $post _fields[' username '] = ' lxvoip '; $post _fields[' password '] = ' 88888888 '; Safety 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 ('/ /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);

Take the cookie above to get the page content you need to log in to see $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);

  • 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.