php curl跨域cookie_php中的CURL函数远程跨域处理Cookie功能实现

该博客介绍了在TP5框架中如何利用curl进行跨域请求并处理cookie。首先,通过curl发送请求获取token值,然后在用户登录方法中,设置cookie并将其应用于后续的curl请求。在其他方法中,展示如何读取并使用已设置的cookie进行请求。这个例子对于理解PHP在API调用中管理cookie跨域问题非常有帮助。
摘要由CSDN通过智能技术生成

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的方式,分享给大家!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值