错误404 - 请求的资源不可用。[PHP]

时间:2015-08-12 12:30:00

标签: php curl

我在REST API上发出了一些CURL请求,如下所示:

$data = array (
    "caseNumber" => "123456789" 
    );

// json encode data

$data_string = json_encode($data); 

// the token

$token = 'eyJhbGciOiJIUzI1NiJ9';


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host.$path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/x-www-form-urlencoded',                                                                                
    'Authorization: Bearer '.$token
));       

// execute the request
$output = curl_exec($ch);   
echo $output;

一般来说,从我的观点来看,它看起来真的很好,我怎么试试这个ECHO只是踢阿帕奇屁股。

我已经尝试了var_dump()print(),但结果仍然相同,没有任何更好的描述,我也不知道如何调试它。 有人可以提供建议或在这里看到我的代码中有错误的人吗? 感谢

1 个答案:

答案 0 :(得分:1)

$output应该是一个布尔值,表示请求成功或失败。它不会包含任何其他内容。为了得到操作的结果,尝试这样的事情:

$output = curl_exec($ch);

if ($output === false) {
    print 'Error: ' . curl_error($ch);
} else {
    print 'Request completed. Output: ' . $output;
}

// Close handle
curl_close($ch);