-
- //Initialize a curl object
- $curl = curl_init();
- //Set the URL you need to crawl
- curl_setopt($curl, curlopt_url, 'http://bbs.it -home.org');
- //Set header
- curl_setopt($curl, curlopt_header, 1);
- //Set curl parameters to ask whether the results are saved in a string or output to the screen.
- curl_setopt($curl, curlopt_returntransfer, 1);
- //Run curl and request the webpage
- $data = curl_exec($curl);
- //Close url request
- curl_close($curl);
- //Display the obtained data
- var_dump($data);
- ?>
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
-
- $phonenumber ='13812345678';
- $message ='thisMessagewasgeneratedbycurlandphp';
- $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); ;
- curl_close($ch);
- ?>
-
Copy code
Example 3: Using a proxy server
Use a proxy server
- $ch = curl_init();
- curl_setopt($ch, curlopt_url, 'http://bbs.it-home.org');
- curl_setopt($ch, curlopt_header, 1 );
- curl_setopt($ch, curlopt_returntransfer, 1);
- curl_setopt($ch, curlopt_httpproxytunnel, 1); d ,'user:password');
- $data = curl_exec();
- curl_close($ch);
- ?>
-
-
- 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);
|