PHP测试新浪微博简版代码(测试通过) - Smart-Team/Spider GitHub Wiki

测试1:

<?php



//login weibo



$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:26.0) Gecko/20100101 Firefox/26.0\r\n" .
              "Cookie: gsid_CTandWM=4uoFa9c610AHfGjJ9SRS96Ajy2d\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://m.weibo.cn/index/feed?format=cards&page=3&next_cursor=3752836900829687&uicode=20000174&rl=1', false, $context);



print_r($file);


?>

测试2:

<?php



//login weibo


crawl('3752897546377675');

function crawl($pre_id)
{
	static $i=0;//循环深度

	$opts = array(
	  'http'=>array(
	    'method'=>"GET",
	    'header'=>"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:26.0) Gecko/20100101 Firefox/26.0\r\n" .
	              "Cookie: gsid_CTandWM=4uoFa9c610AHfGjJ9SRS96Ajy2d\r\n"
	  )
	);

	$context = stream_context_create($opts);

	// Open the file using the HTTP headers set above
	$file = file_get_contents('http://m.weibo.cn/index/feed?format=cards&uicode=20000174&rl=1&next_cursor='.$pre_id, false, $context);


	if($file)
	{
		$data = json_decode($file, true);

		if($data && isset($data['next_cursor']))
		{
			
			//parsedata
			if(isset($data['cards']) && isset($data['cards'][0]['card_group']))
			{
				$items = $data['cards'][0]['card_group'];
				if($items && count($items))
				{
					foreach($items as $vv)
					{
						$v = $vv['mblog'];
						$info['text'] = $v['text'];
						$info['source'] = $v['source'];
						$info['user'] = $v['user']['screen_name'];
						$info['user_id'] = $v['user']['id'];

						print_r($v['text']);

						saveData(json_encode($info, JSON_UNESCAPED_UNICODE));
					}
				}
			}


			//next page
			if(++$i> 100)
			{
				return;
			}
			crawl($data['next_cursor']);

		}else{
			echo "\nend.....\n";
		}

	}
}


function saveData($str)
{

	print_r("$str \n");
	file_put_contents("weibo.data", "$str\n", FILE_APPEND);
}