php使用curl登录人人网抓取信息

当初做一个网站,需要实现“用户分享本站,当有人点击时,用户的头像和链接排到第一位”这样的功能。于是找php登录人人网的方法,结果找到的函数都是失效的,原因是人人网的登录过程是多层301跳转。于是就自己写了一个方法。
可惜的是,这个东西并没起到吸引流量的作用。根本没人用这个,所以我把这个功能去掉了。

$post_fields['email'] = 'xxx@126.com'; //人人网用户名
$post_fields['password'] = 'xxxxxxx';  //人人网密码
$post_fields["isplogin"]="true";
 
function getlocation($str){
	if(strpos($str,"Location: ")){
		$array=explode("Location: ",$str);
		$array=explode("\n",$array[1]);
		return $array[0];
	}else{
		return '';
	}
}
function open($url,$post_fields,$cookie=''){
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
	curl_setopt($ch, CURLOPT_HEADER, true); //启用时会将头文件的信息作为数据流输出。
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //讲curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); //启用时会将服务器服务器返回的“Location:”放在header中递归的返回给服务器,使用CURLOPT_MAXREDIRS可以限定递归返回的数量。
	curl_setopt($ch, CURLOPT_AUTOREFERER, 1); //自动设置header中的referer信息
	curl_setopt($ch, CURLOPT_POST, 1); //启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); //在HTTP中的“POST”操作。如果要传送一个文件,需要一个@开头的文件名
	curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
	$cookie_file = tempnam('../click','cookie');//cookie文件存放在网站根目录的click文件夹下
	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); //连接关闭以后,存放cookie信息的文件名称
	$contents=curl_exec($ch);
	curl_close($ch);
	$url = trim(urldecode(getlocation($contents)));
	if($url!=''){
		$contents = open($url,$post_fields,$cookie_file); //如果存在301跳转,就跳到新地址。
	}
	if($cookie!=''&&file_exists($cookie)){
		unlink($cookie);
	}
	if($cookie_file!=''&&file_exists($cookie_file)){
		unlink($cookie_file);
	}
	return $contents;
}
Spancerlaria2021-02-12 14:50:48

Perfectly voiced indeed. !

Alvencus2021-02-12 16:51:53

Truly many of helpful material!

Alvencus2021-02-12 17:48:57

You have made your point pretty clearly..

google2021-02-13 23:53:13

I was curious if you ever thought of changing the layout of your website?
Its very well written; I love what youve
got to say. But maybe you could a little more in the way of
content so people could connect with it better. Youve got an awful lot of text for only having one or two pictures.

Maybe you could space it out better?

datazione di skypethegame2021-02-14 20:55:13

Great post. I was checking continuously this blog and I am
impressed! Extremely useful info specially the last part 🙂 I care for such information a lot.
I was looking for this particular info for a long time.
Thank you and good luck.

google2021-02-14 23:17:28

Have you ever considered writing an ebook or guest authoring on other blogs?
I have a blog based upon on the same information you discuss and would love to have you share some stories/information. I know my readers would value your work.
If you’re even remotely interested, feel free to shoot me an e-mail.

chorizanthe validating2021-02-15 4:34:23

Thank you a bunch for sharing this with all people you actually
understand what you’re speaking approximately! Bookmarked.
Kindly additionally discuss with my site =). We will have a link change arrangement between us

porn2021-02-16 22:19:26

Hi there! Do you know if they make any plugins to help with SEO?
I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains.

If you know of any please share. Cheers!

casino online2021-02-17 3:32:30

What i do not understood is in reality how you’re not actually much
more smartly-liked than you might be right now.

You are so intelligent. You recognize thus considerably in terms of this
matter, produced me in my view believe it from a lot of numerous angles.

Its like women and men don’t seem to be interested unless it’s something
to do with Lady gaga! Your own stuffs excellent.
At all times take care of it up!

fracture dating2021-02-18 18:56:31

Can I simply say what a comfort to discover someone who genuinely
knows what they are talking about on the internet. You definitely realize how to bring an issue to light
and make it important. More people should check this
out and understand this side of your story.
I can’t believe you’re not more popular since you surely possess the gift.

6hookup datazione2021-02-22 0:47:13

Thanks for finally writing about >php使用curl登录人人网抓取信息 <Loved it!

news2021-02-24 17:59:23

Hello! Would you mind if I share your blog with my facebook group?
There’s a lot of people that I think would really appreciate your content.
Please let me know. Thanks

验证码