Table of Contents
PHP Curl simulates logging into the WeChat public platform and Sina Weibo example code, curl example
Articles you may be interested in:
Home Backend Development PHP Tutorial PHP Curl simulates login to WeChat public platform, Sina Weibo example code, curl example_PHP tutorial

PHP Curl simulates login to WeChat public platform, Sina Weibo example code, curl example_PHP tutorial

Jul 12, 2016 am 08:59 AM
curl php Micro-channel public platform Sina Weibo Simulated login

PHP Curl simulates logging into the WeChat public platform and Sina Weibo example code, curl example

Open the curl configuration before using curl. The specific method can be found on Baidu. Turn on the curl extension. The password is encrypted with md5. This has been successfully tested. Just change the user and password to yours.

The following code introduces how PHP uses curl to simulate logging into the WeChat public platform. The specific code is as follows:

<&#63;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); 
&#63;>
Copy after login

Use PHP CURL to simulate logging into Sina Weibo

Sometimes we get some Sina Weibo data, but we don’t want to use the API, so we have to use simulated login.

I found that the CURL simulation login code that could be used before was invalid. I Googled it and found that many people had encountered this problem. But I couldn’t find a solution, so I did some research myself and found the reason.

It may be because Sina restricts simulated login. With the same login parameters, everything is normal when logging in through the web page, but when logging in using CURL, the returned COOKIES are temporary.

So it seems that the login was successful and the user information was obtained, but the status is still not logged in when accessing again. My solution is relatively simple, just modify the validity of COOKIES directly.

Attached is the PHP code that I tested and passed as follows. I hope it will be useful to friends who have the same problem. If you have a better solution, please share it.

I found that as long as the CURLOPT_COOKIESESSION parameter is not set, there is no need to modify COOKIE_FILE.

<&#63;php
class 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&#63;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&#63;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;
}
}
&#63;>
Copy after login

The above content introduces PHP Curl simulated login to WeChat public platform and Sina Weibo example code. I hope this article will be helpful to everyone.

Articles you may be interested in:

  • PHP CURL simulates login to Sina Weibo and captures page content. Developed based on EaglePHP framework
  • An example of PHP using curl to simulate login and collect the page.
  • PHP CURL method of obtaining cookies to simulate login
  • Use CURL in PHP to simulate login and obtain data instances
  • PHP uses CURL to simulate login to websites with verification codes Method
  • How PHP reads CURL to simulate login and generates a cookie file
  • PHP uses CURL to simulate login method
  • PHP curl simulates login to a website with verification code

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1097315.htmlTechArticlePHP Curl simulates logging into the WeChat public platform and Sina Weibo instance code. Before using curl in the curl instance, open the curl configuration. The specific method can be found on Baidu: enable curl extension. The password is...
Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles