php curl_init function usage example

WBOY
Release: 2016-07-25 08:54:24
Original
1041 people have browsed it
  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 ask whether the results are 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 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 ='thisMessagewasgeneratedbycurlandphp';
  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. curl_close($ch);
  11. ?>
Copy code
Example 3: Using a proxy server Use 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_httpproxytunnel, 1); d ,'user:password');
  6. $data = curl_exec();
  7. curl_close($ch);
  8. ?>
  9. Copy code
Example 4: Simulated login curl simulates logging in to the discuz program, suitable for dz7.0. Just change username to your username and userpass to your password. curl simulates login discuz program
!extension_loaded('curl') && 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 be modified $post_fields['loginfield'] = 'username'; $post_fields['loginsubmit'] = 'true'; //Username and password must be filled in $post_fields['username'] = 'lxvoip'; $post_fields['password'] = '88888888'; //Security Question $post_fields['questionid'] = 0; $post_fields['answer'] = ''; //@todo verification code $post_fields['seccoverify'] = '';

//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 cookies $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);

//Use the cookie obtained above to obtain the page content that needs to be logged in to view $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);

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template