- 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);
- Sets the curl parameter, which requires the result to be saved to a string or output to the screen.
- curl_setopt ($curl, Curlopt_returntransfer, 1);
- Run Curl, request a Web page
- $data = curl_exec ($curl);
- Close URL Request
- Curl_close ($curl);
- Show the data obtained
- Var_dump ($data);
- ?>
Copy CodeExample 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 is 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);
- $data = Curl_exec ();
- Curl_close ($ch);
- ?>
Copy CodeExample 3: Using Proxy server with 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);
- curl_setopt ($ch, Curlopt_proxy, ' proxy.lxvoip.com:1080 ');
- curl_setopt ($ch, curlopt_proxyuserpwd, ' User:password ');
- $data = Curl_exec ();
- Curl_close ($ch);
- ?>
Copy CodeExample 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); |