search
HomeBackend DevelopmentPHP TutorialPHP Curl模拟登录微信公众平台、新浪微博

使用curl之前先打开curl配置,具体方式百度一下就知道,开启curl扩展。密码用md5加密,这是经过测试成功的,把用户跟密码改成你的就行了。

下面一段代码给大家介绍php使用curl模拟登录微信公众平台,具体代码如下所示:

<?php //模拟微信登入 $cookie_file = tempnam('./temp','cookie'); $login_url = 'https://mp.weixin.qq.com/cgi-bin/login'; $pwd = md5("********");$data = "f=json&imgcode=&pwd=$pwd&username=*****@***.com"; $ch = curl_init($login_url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ch,CURLOPT_REFERER,'https://mp.weixin.qq.com'); curl_setopt($ch,CURLOPT_POSTFIELDS,$data); $content = curl_exec($ch); curl_close($ch); $newurl = json_decode($content,1); //var_dump($newurl);//exit;$newurl = $newurl['redirect_url']; //获取登入后页面的源码 $go_url = 'https://mp.weixin.qq.com'.$newurl; $ch = curl_init($go_url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $content = curl_exec($ch); //var_dump(curl_error($ch)); print_r($content); curl_close($ch); ?>

使用 PHP CURL 模拟登录新浪微博

有时候我们获取一些新浪微博的数据,但又不想使用API,只好使用模拟登录了.

发现以前可以使用的CURL模拟登录代码失效了,Google一下,发现有很多人碰到这个问题.但是没有找到解决方法,所以就自己研究了一下,发现了原因.

可能是因为新浪限制了不允许模拟登录,同样的登录参数,用网页登录一切正常,用CURL登录,返回的COOKIES竟然是临时的.

所以看起来是登录成功了,并且获取到了用户信息,但是再次访问还是未登录状态.我的解决方法比较简单,直接修改COOKIES的时效这样就行了.

附上我自己测试通过的PHP代码如下,希望有对有同样问题的朋友有用,如果你有更好的方案欢迎分享一下.

发现只要不设置CURLOPT_COOKIESESSION参数就行了,不需要修改COOKIE_FILE.

<?phpclass sina{/*一个简单的新浪微搏curl模拟登录类. 来源: http://chenall.net/post/sina_curl_login/ 使用方法:http函数是一个简单的curl封装函数,需要自己去实现,http函数原型如下:http($url,$post_data = null)返回网页内容.第一个参数$url,就是要访问的url地址,$post_data是post数据,如果为空,则代表GET访问.1.使用加密后密码登录 加密方法: sha1(sha1($pass))$sina = new sina($username,$sha1pass)2.直接使用原始密码登录$sina = new sina($username,$sha1pass,0)执行之后如果$sina->status非空,则登录成功,否则登录失败.登录成功之后,你就可以直接继续使用http函数来访问其它内容.使用 unset($sina) 会自动注销登录.*/public $status;function __construct($su,$sp,$flags = 1) {$this->status = $this->login($su,$sp,$flags);}function __destruct(){//注销登录$this->logout();}function logout(){http("http://weibo.com/logout.php");unset($this->status);}/*不需要了,只要不设置HTTP函数中不设置CURLOPT_COOKIESESSION参数就行了,要设可以设为false.function ResetCookie()//重置相关cookie{global $cookie_file;$str = file_get_contents($cookie_file);$t = time()+3600;//设置cookie有效时间一个小时$str = preg_replace("/\t0\t/", "\t".$t."\t", $str);$f = fopen($cookie_file,"w");fwrite($f,$str);fclose($f);}*/function login($su,$sp,$flags = 0){$su = urlencode(base64_encode($su));$data = http("http://login.sina.com.cn/sso/prelogin.php?entry=miniblog&client=ssologin.js&user=".$su);if (empty($data))return null;//$data = substr($data,35,-1);$data = json_decode($data);if ($data->retcode != 0)return null;if ($flags == 0)$sp = sha1(sha1($sp));$sp .= strval($data->servertime).$data->nonce;$sp = sha1($sp);$data = "url=http%3A%2F%2Fweibo.com%2Fajaxlogin.php%3F&returntype=META&ssosimplelogin=1&su=".$su.'&service=miniblog&servertime='.$data->servertime."&nonce=".$data->nonce.'&pwencode=wsse&sp='.$sp;$data = http("http://login.sina.com.cn/sso/login.php?client=ssologin.js",$data);//$this->ResetCookie();if (preg_match("/location\.replace\('(.*)'\)/",$data,$url)){$data = http($url[1]);//$this->ResetCookie();$data = json_decode(substr($data,1,-2));if ($data->result == true)return $data->userinfo;}return null;}}?>

以上内容给大家介绍了PHP Curl模拟登录微信公众平台、新浪微博实例代码,希望本文所述对大家有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram APIIntroduction to the Instagram APIMar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor