简体   繁体   English

PHP CURL错误403

[英]PHP CURL error 403

I'm trying to get some data in CURL. 我正在尝试在CURL中获取一些数据。 For this i need to do it in 2 step : 为此,我需要在2步中做到这一点:
-get my token code and custom url -获取我的令牌代码和自定义网址
-Fetch the data i need -获取我需要的数据

for the first part i did 我做的第一部分

$url = 'https://url.com/login';

$params = array( "KeyName" => "KeyValue");
$params = json_encode($params);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $params);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

//recovery of token and URL
$response = curl_exec($ch);
$value = json_decode($response);
$value_token = $value->data->token;
$url = $value->data->url;

i get both values without a problem but when i try the next step i get an error 403 . 我得到两个值都没有问题,但是当我尝试下一步时, error 403 here is my code : 这是我的代码:

$params = array( "X-Session-Token" => "$value_token");
$params = json_encode($params);
$url = $url ."/api";

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $params);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE);
echo $httpCode ."<- code http<br>";
if ($response === false) $response = curl_error($ch);
    echo stripslashes($response) ."<br>"; //return 1
curl_close($ch);
$val = json_decode($response);

I've already done it succesfully in command promt. 我已经在命令提示符中成功完成了此操作。 here is the command prompt for the second request : 这是第二个请求的命令提示符:

curl "https://custom-url.com/api" \
-­H "Accept: application/json, text/*;q=0.2" \
­-H "Accept-­Charset: utf­8" \
­-H "Accept-­Encoding: gzip, deflate" \
­-H "Access-­Control­-Request-­Headers: x-­session-­token" \
­-H "X­-Session-­Token: MyToken" \
--­­compressed

Update 更新资料
here is the request with the updated header : 这是带有更新的标头的请求:

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Session-Token' => $value_token,'Access-Control-Request-Headers' => 'X-Session-Token', 'Accept' => 'application/json, text/*;q=0.2', 'Accept-Encoding' => 'gzip, deflate', 'Access-Control-Allow-Origin' =>'*'));
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

curl_exec($ch);

$info = curl_getinfo($ch);
print_r("<br>print : ". $info['request_header']);

print_r return : POST /api HTTP/1.1 Host: website.com Accept: / Content-Type: application/x-www-form-urlencoded Expect: 100-continue print_r返回值:POST / api HTTP / 1.1主机:website.com接受: /内容类型:application / x-www-form-urlencoded预期:100-继续

Have you tried adding a request header? 您是否尝试添加请求标头? Try adding curl_setopt($ch, CURLOPT_HEADER, true); 尝试添加curl_setopt($ch, CURLOPT_HEADER, true); and see if that helps. 看看是否有帮助。

If we are talking of session tokens here, every API would have a different standard for accepting tokens. 如果我们在这里谈论会话令牌,则每个API都有不同的接受令牌标准。 Some are encoded as Bearer tokens, some need to be in the URL, and some need a POST variable. 有些编码为Bearer令牌,有些需要在URL中,有些需要POST变量。

This sounds like a case where it's a special header: 这听起来像是特殊标头的情况:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Session-Token' => $value_token
));

or, if you want to exactly match the commandline CURL request: 或者,如果您想完全匹配命令行CURL请求:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Session-Token' => $value_token,
    'Access-­Control­-Request-­Headers' => 'X-Session-Token'
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM