PHP CURL simulates login and collects data

 //This page is GBK, because php100 is gbk encoding
 $cookie_file = tempnam('./temp', 'cookie');
  //generate a temporary file
 $login_url = "";
 $post_fields = 'cktime=3600&step=2&pwuser=xxx&pwpwd=xxxxxx';
 $ch = curl_init($login_url);
 curl_setopt($ch, CURLOPT_HEADER, 0); //whether to display the header file
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set whether the returned data is automatically displayed
 curl_setopt($ch, CURLOPT_POST, 1);
 //Save the logged-in cookie in the file of $cookie_file, read this file below
 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); //Set fields
 $url = ''; //The page displayed after login
 //$url = '';
 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
  //display page
 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //Read the file content
 $content = $content2 = curl_exec($ch);
 //Regular matching required value (grab) collection
 preg_match('/<span class="f24 b s2 mb5" style="line-height:1.1;font-family:Georgia;">(.*)<\/span>/', $content, $arr);
 //echo $arr[1];
 preg_match('/<li class="w mb5">Gold: (.*)<\/li>/', $content2, $arr2);
 //echo $arr2[1];

Leave a Reply

Your email address will not be published. Required fields are marked *