Curl请求适用于Rested chrome扩展,但不适用于php

时间:2017-09-25 11:21:10

标签: php curl

大家,我正在从其他网站请求数据但是我无法通过curl请求它显示错误500但是当我通过chrome RESTED扩展执行相同的请求时它会显示输出

$url = "XXXXXXXXXXX";

// what post fields?
$fields = array(
    'roll_number' => '123456',
'full_name' => 'aaaaaa',
'mother_name' => 'bbbbbbb',
'_token' => 'xxxxxxxxxxxxxxxx'
);

// build the urlencoded data
$postvars = http_build_query($fields);

// open connection


            $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postvars);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response  = curl_exec($ch);
    curl_close($ch);

    echo $response;

请求应该是post方法,数据应该是JSON编码或URL编码我已经尝试了两种,但没有一种工作

1 个答案:

答案 0 :(得分:0)

不确定这是否是原因,但您的HTTPHEADER不应该application/json?此外,您未将数据编码为JSON - 您可能需要将$postvars = http_build_query($fields);更改为$postvars = json_encode($fields);

我还要看一下https://lornajane.net/posts/2011/posting-json-data-with-php-curl