PHP CURL错误403

时间:2018-07-24 11:41:05

标签: php curl

我正在尝试获取CURL中的一些数据。为此,我需要分2步进行操作:
-获取我的令牌代码和自定义网址
-获取我需要的数据

我做的第一部分

$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;

我得到两个值都没有问题,但是当我尝试下一步时,我得到了error 403。这是我的代码:

$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);

我已经在命令提示符中成功完成了它。 这是第二个请求的命令提示符:

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

更新
这是带有更新后的标头的请求:

$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返回:POST / api HTTP / 1.1主机:website.com接受: / 内容类型:application / x-www-form-urlencoded预期:100-继续

2 个答案:

答案 0 :(得分:0)

您是否尝试添加请求标头? 尝试添加'data.frame': 2 obs. of 2 variables: $ var : Factor w/ 2 levels "abc_book","abc_toy": 1 2 $ varconsistsof: chr "abc_book_E1_C1,abc_book_E1_C2,abc_book_E1_C3" "abc_toy_E1_R1,abc_toy_E1_R2" ,看看是否有帮助。

答案 1 :(得分:0)

如果我们在这里谈论会话令牌,则每个API都有不同的接受令牌标准。 有些编码为Bearer令牌,有些需要在URL中,有些需要POST变量。

这听起来像是特殊标头的情况:

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

或者,如果您想完全匹配命令行CURL请求:

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