在PHP curl请求三方接口的时候 登陆接口后 发现怎么请求都环保取不到数据 后来经过查询 发现没有携带本网站的cookie,解决方法如下
首先是三方接口类
public function __construct($name = "", $password = "",$url="")
{
if (!empty($name)) {
$this->name = $name;
}
if (!empty($password)) {
$this->password = $password;
}
if (!empty($url)) {
$this->Url = $url;
}
}
/**
* 登录获取当前cookie
* @return bool|string
*/
function login()
{
$url = $this->Url. $this->name . '&password=' . $this->password . '&loginType=Ajax';
$ch = curl_init();
//定义/tmp.cookie 路径 在public/uplodes/下
$cookie_file = '.'.DS.'uploads'. DS ."/tmp.cookie";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//获取cookie 放入到文件中
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
然后 下次使用 curl的时候
$cookie_file = '.'.DS.'uploads'. DS ."/tmp.cookie";
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
就可以获取到数据了