tp5框架中使用curl函数跨域处理cookie保存的问题:
/**
*
* @file Api.php
* @date 2018-7-23
* @author zhu
*/
namespace app\api\controller;
use think\Controller;
class Api extends Controller{
/**
* 获取token值
* @return type
*/
public static function getToken(){
$url = '请求的url地址';
$data = array(
"_app_name"=>"参数",
"_secret"=>"参数",
"g"=>"参数",
"m"=>"参数",
"c"=>"参数"
);
$data = json_encode($data);
$headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
/**
* 用户登录
* @return type
*/
public function login()
{
$userName = input("post.phone");
$password = input("post.password");
$returnData = Api::getToken();
$return = (json_decode($returnData,true));
$url = '请求的url地址';
$data = array(
"_app_name" => "参数",
"_Version" => "2.0",
"g" => "参数",
"m" => "参数",
"c" => "参数",
"_token" => $return['content']['access_token'],
"user_name" => $userName,
"password" => $password,
);
$data = json_encode($data);
$headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
list($header, $body) = explode("\r\n\r\n", $output, 2);
preg_match("/ZJ-LVEI3DRhYauth\=(.*?)\;/", $header, $matches);
if($matches){
// 设置Cookie 有效期为 604800
cookie('Set-Cookie',$matches['0'],604800);
}
return json_decode($body,true);
}
在其他方法中使用cookie的方式代码如下:
/**
* 实名认证列表
* @return type
*/
public function certification()
{
$phone = input("psot.phone");
$name = input("post.name");
$idcard = input("post.idcardnumber");
$attachinfo = input("post.attachinfo");
$certificatephotosinfo = input("post.certificatephotosinfo");
$returnData = Api::getToken();
$return = (json_decode($returnData,true));
$url = '请求的url地址';
$data = array(
"_app_name" => "参数",
"_Version" => "2.0",
"g" => "参数",
"m" => "参数",
"c" => "参数",
"_token" => $return['content']['access_token'],
"category_id" => 1,
"phone" => $phone,
"realname" => $name,
"idcard" => $idcard,
"attach_info" => $attachinfo,
"certificate_photos_info" => $certificatephotosinfo,
);
$data = json_encode($data);
$headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
curl_setopt ($curl, CURLOPT_COOKIE , cookie('Set-Cookie')); //使用cookie
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return json_decode($output);
}
}
?>
远程跨域请求相应的接口时,php客户端处理cookie的方式,分享给大家!