-
- //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 require the results to be 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 the 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 ='This message was generated by curl and php';
- $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); exec() ;
- curl_close($ch);
- ?>
-
Copy code
Example 3, using 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_HTTPPROXYTU NNEL, 1); CURLOPT_PROXYUSERPWD ,'user:password');
- $data = curl_exec();
- curl_close($ch);
- ?>
-
-
- 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.'); $discuz_url = 'http://bbs.it-home.org';//Forum address
- $login_url = $discuz_url .'/logging.php?action=login';//Login page address
- $get_url = $discuz_url .'/ my.php?item=threads'; //My post
$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'] = ''; < /p>
//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.');
- } < ;/p>
//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_POSTFIEL DS, $post_fields) ;
- curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
- curl_exec($ch);
- curl_close($ch);
//You need to log in to obtain the COOKIE obtained above Viewed page content
- $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);
-
-
-
- Copy code
|