Home  >  Article  >  Backend Development  >  Detailed explanation of php curl_init function usage tutorial

Detailed explanation of php curl_init function usage tutorial

WBOY
WBOYOriginal
2016-07-25 08:52:281136browse
  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. //Set cURL parameters to require the results to be saved in a string or output to the screen.
  8. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  9. //Run cURL and request the webpage
  10. $data = curl_exec($curl);
  11. //Close the URL request
  12. curl_close($curl);
  13. //Display the obtained data
  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 was 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); exec() ;
  10. curl_close($ch);
  11. ?>
Copy code
Example 3, using a 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_HTTPPROXYTU NNEL, 1); CURLOPT_PROXYUSERPWD ,'user:password');
  6. $data = curl_exec();
  7. curl_close($ch);
  8. ?>
  9. Copy code
Example 4, simulate login. Curl simulates the login discuz program, suitable for DZ7.0. Change username to your own username and userpass to your own password. Curl simulated login discuz program

    !extension_loaded('curl') && die('The curl extension is not loaded.');

  1. $discuz_url = 'http://bbs.it-home.org';//Forum address

  2. $login_url = $discuz_url .'/logging.php?action=login';//Login page address
  3. $get_url = $discuz_url .'/ my.php?item=threads'; //My post

  4. $post_fields = array();

  5. //The following two items do not need to be modified
  6. $post_fields['loginfield'] = ' username';
  7. $post_fields['loginsubmit'] = 'true';
  8. //Username and password, must be filled in
  9. $post_fields['username'] = 'lxvoip';
  10. $post_fields['password'] = '88888888' ;
  11. //Security question
  12. $post_fields['questionid'] = 0;
  13. $post_fields['answer'] = '';
  14. //@todo verification code
  15. $post_fields['seccoverify'] = ''; < /p>
  16. //Get form FORMHASH

  17. $ch = curl_init($login_url);
  18. curl_setopt($ch, CURLOPT_HEADER, 0);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  20. $contents = curl_exec ($ch);
  21. curl_close($ch);
  22. preg_match('// i', $contents, $matches);
  23. if(!empty($matches)) {
  24. $formhash = $matches[1];
  25. } else {
  26. die('Not found the forumhash.');
  27. } < ;/p>
  28. //POST data, get COOKIE

  29. $cookie_file = dirname(__FILE__) . '/cookie.txt';
  30. //$cookie_file = tempnam('/tmp');
  31. $ch = curl_init($login_url);
  32. curl_setopt($ch, CURLOPT_HEADER, 0);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  34. curl_setopt($ch, CURLOPT_POST, 1);
  35. curl_setopt($ch, CURLOPT_POSTFIEL DS, $post_fields) ;
  36. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  37. curl_exec($ch);
  38. curl_close($ch);

  39. //You need to log in to obtain the COOKIE obtained above Viewed page content

  40. $ch = curl_init($get_url);
  41. curl_setopt($ch, CURLOPT_HEADER, 0);
  42. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
  43. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  44. $ contents = curl_exec($ch);
  45. curl_close($ch);

  46. var_dump($contents);

  47. Copy code
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn