This morning, the editor opened our PHP Chinese website (www.php.cn) and saw that some netizens were asking PHP to obtain page data. They need to bring cookies. They don’t know how to write it. Our website’s full-time engineers responded immediately. And gave a solution. Here I share it with everyone to learn how to obtain web page data in PHP. Below is the original question from a netizen.
php get Web page data I want to write a web page data collection with cookies. I don’t know how to write it in PHP. Is there any expert who can give me an answer? Collection URL = “https://steamcommunity.com/tradeoffer/new /partnerinventory/?sessionid=29475e48124c520cf70060dc&partner=76561198124311480&appid=433850&contextid=1”
cookie="sessionid=29475e48124c520cf70060dc;steamLoginSecure=765 61198117047952%7C%7C512C7DB04B6A9719E1673019C075AA5ECF2B5032"
Can you write an example with explanation? Thank you. This cookie has a time limit. If it expires, just write an example.
Our staff provides the use of CURL to violate the COOKIE. You can read the next step. Regarding the types of curl-php Chinese website provides many types of downloads http://www.php.cn/xiazai/leiku/curl
<?php error_reporting(E_ALL); ini_set('display_errors','1'); ignore_user_abort(); set_time_limit(0); $cookie_path='./'; $vars['username']='wang'; $vars['password']='123456'; $method_post=true; $url='http://ceshi.php.cn/user/usertop_login.asp'; $ch=curl_init(); $params[CURLOPT_URL]=$url; $params[CURLOPT_HEADER]=0;//是否显示http头信息 $params[CURLOPT_RETURNTRANSFER]=true; $params[CURLOPT_FOLLOWLOCATION]=0; $params[CURLOPT_USERAGENT]='Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0'; //$params[CURLOPT_SSL_VERIFYPEER]=false; //$params[CURLOPT_SSL_VERIFYHOST]=false; $postfields=''; foreach($vars as $k=>$v){ $postfields.=urlencode($k).'='.urlencode($v).'&'; } $params[CURLOPT_POST]=true; $params[CURLOPT_POSTFIELDS]=$postfields; if(isset($_COOKIE['cookie_jar']) && ($_COOKIE['cookie_jar'] || is_file($_COOKIE['cookie_jar']))){ $params[CURLOPT_COOKIEFILE]=$_COOKIE['cookie_jar']; }else{ $cookie_jar=tempnam($cookie_path,'cookie');//产生一个cookie文件 $params[CURLOPT_COOKIEJAR]=$cookie_jar;//写入cookie信息 setcookie('cookie_jar',$cookie_jar);//保存cookie路径 } curl_setopt_array($ch,$params); $content=curl_exec($ch); //var_dump(strip_tags($content)); //第二步 $params[CURLOPT_FOLLOWLOCATION]=true; $nexturl='http://ceshi.php.cn/user/vpsadm2.asp?id=100568&go=c'; $params[CURLOPT_URL]=$nexturl; $params[CURLOPT_POSTFIELDS]=''; curl_setopt_array($ch,$params); $content=curl_exec($ch); sleep(5); //第三步 $nexturl='http://ceshi.php.cn/vpsadm/selfvpsmodifyendtime.asp'; $params[CURLOPT_URL]=$nexturl; $params[CURLOPT_POSTFIELDS]='year=9001&moneynow=10&id=100568&'; curl_setopt_array($ch,$params); $content=curl_exec($ch); echo strip_tags($content); curl_close($ch);