2
回答
php CURL模似WAP版移动登录问题,请大家过来看看
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

上面要求模似WAP版移动阅读登录而实现里面各种功能
WAP版移动阅读登录页网址是 http://wap.cmread.com/sso/auth?e ... ;e_c=0000&e_s=0
打开这个请先修改浏览器user agent再访问这个
我折腾快一天了,运行一直显示返回字样。换用163邮箱登录测试一下没问题了。
上代码
<?php
header("Content-Type:text/html;charset=utf-8");
$url = "http://wap.cmread.com/sso/oauth2/login?e_l=2&f=44012&pg=";
$fields = array(
'uname'=>'meiye1988',
'passwd'=>'123123',
'rememberUname'=>'on',
'login'=>'%E7%99%BB%E5%BD%95'
);;
// 用来存放cookie的文件
$cookie_file = tempnam('./temp','cookie');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://wap.cmread.com/r/p/index.jsp?vt=2&f=&pg=');
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; U; Android 4.1.1; zh-cn; MI 2S Build/JRO03L) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 XiaoMi/MiuiBrowser/1.0");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
// 执行操作
$result = curl_exec($ch);

if ($result == NULL) {
        echo "Error:";
        echo curl_errno($ch) . " – " . curl_error($ch) . "";
}else{
        echo $result;
}
curl_close($ch);
?>
请大家帮忙看一下,先谢谢大家
PHP
举报
魅夜
发帖于4年前 2回/3K+阅
共有2个答案 最后回答: 4年前
<?php

$url = "http://wap.cmread.com/sso/oauth2/login?e_l=2&f=44012&pg="; 
$fields = array(
	'uname'=>'meiye1988', 
	'passwd'=>'123123', 
	'rememberUname'=>'on', 
	'login'=>'登入' 
);

$client = new HTTPClient();
$html = $client->get('http://wap.cmread.com/sso/auth?e_p=1&response_type=token&e_l=2&redirect_uri=http%3A%2F%2Fwap.cmread.com%2Fr%2Ff%2Fslr&layout=2&state=succurl%253D%252Fr%252Fp%252Findex.jsp%253Ff%253D3862%2526pg%253D11%2526layout%253D2%2526vt%253D2%26faildurl%253D%252Fr%252Fp%252Findex.jsp%253Ff%253D3862%2526pg%253D11%2526layout%253D2%2526vt%253D2&fr=113&client_id=cmread-wap&e_f=0&e_c=0000&e_s=0');
echo $html, PHP_EOL;
$html = $client->post($url, $fields);
echo $html, PHP_EOL;

Class HTTPClient{
	private $_loginURL = 'http://wap.cmread.com/sso/oauth2/login?e_l=2&f=44012&pg=';
	private $_cookieJarFile='';
	private $_userAgent = 'Mozilla/5.0 (Linux; U; Android 4.1.1; zh-cn; MI 2S Build/JRO03L) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 XiaoMi/MiuiBrowser/1.0';
	public function __construct($cookieJarFile=null, $userAgent=''){
		if(is_null($cookieJarFile) || !file_exists($cookieJarFile)){
			$this->_cookieJarFile = implode( DIRECTORY_SEPARATOR, array(dirname(__FILE__), 'cookie.txt'));
		}
		if(!empty($userAgent)){
			$this->_userAgent = $userAgent;
		}
	}
	
	private function _HttpRequest($HttpMethod, $url, $data, $opt=array()){
		$ch = curl_init();
		$defaultCURLOption = array(
			CURLOPT_URL => $url,
			CURLOPT_COOKIEJAR => $this->_cookieJarFile,
			CURLOPT_COOKIEFILE => $this->_cookieJarFile,
			CURLOPT_USERAGENT => $this->_userAgent,
			CURLOPT_HEADER => true,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_AUTOREFERER => true,
			CURLOPT_FOLLOWLOCATION => false,
		);
		curl_setopt_array($ch, $defaultCURLOption);
		if(!empty($opt)){
			curl_setopt_array($ch, $opt);
		}
		if($HttpMethod == self::HTTP_REQUEST_POST){
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
		}else{
			curl_setopt($ch, CURLOPT_HTTPGET, true);
		}
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
	
	public function get($url, $opt = array()){
		return $this->_HttpRequest(self::HTTP_REQUEST_GET, $url, $opt);
	}
	
	public function post($url, $data, $opt = array()){
		if(is_array($data)){
			$data = http_build_query($data);
		}
		return $this->_HttpRequest(self::HTTP_REQUEST_POST, $url, $data, $opt);
	}
	
	const HTTP_REQUEST_GET = CURLOPT_HTTPGET;
	const HTTP_REQUEST_POST = CURLOPT_POST;
}

兄弟只能帮你到这里了。自己看吧!我这里测试的结果是成功了。

有不明白再问吧!


--- 共有 1 条评论 ---
魅夜这里可以用了,太感谢你 4年前 回复
顶部