- // Initialize a curl object
- $ Curl = curl_init ();
- // Set the url you want to capture
- Curl_setopt ($ curl, curlopt_url, 'http: // bbs.it-home.org ');
- // Set the header
- Curl_setopt ($ curl, curlopt_header, 1 );
- // Set the curl parameter to save the result to the string or output to the screen.
- Curl_setopt ($ curl, curlopt_returntransfer, 1 );
- // Run curl to request the webpage
- $ Data = curl_exec ($ curl );
- // Close the url request
- Curl_close ($ curl );
- // Display the obtained data
- Var_dump ($ data );
- ?>
Example 2: post data sendsms. php can accept two form fields: phone number and text message content. Post data
- $ Phonenumber = '000000 ';
- $ 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 );
- $ Data = curl_exec ();
- Curl_close ($ ch );
- ?>
Example 3: 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 );
- Curl_setopt ($ ch, curlopt_proxy, 'proxy .lxvoip.com: 1080 ');
- Curl_setopt ($ ch, curlopt_proxyuserpwd, 'User: password ');
- $ Data = curl_exec ();
- Curl_close ($ ch );
- ?>
Example 4: simulate curl logon to simulate logon to the discuz program. this is suitable for dz7.0. change username to your username and change userpass to your password. curl simulates login to the 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 '; // logon page address $ get_url = $ discuz_url.'/my. php? Item = threads'; // my post $ Post_fields = array (); // You do not need to modify the following two items: $ post_fields ['loginfield'] = 'username'; $ post_fields ['loginsubmit '] = 'true '; // username and password, which must be set to $ post_fields ['username'] = 'lxvoip '; $ post_fields ['password'] = '123 '; // security question $ post_fields ['questionid'] = 0; $ post_fields ['answer'] = ''; // @ todo verification code $ post_fields ['seccodeverify '] = ''; // Obtain the 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, batch, 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 ); // Obtain the page content that requires logon with the cookie obtained above $ 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 ); |