使用curl调用Rest API PHP时出现404错误

时间:2018-10-15 07:54:25

标签: php api

我遇到了错误:

HTTP/1.1 404 Not Found Server: Apache-Coyote/1.1 Content-Type: text/html;charset=utf-8 Content-Language: en Content-Length: 1005 

下面是我使用PHP的代码。任何人都可以提出调用Rest API的最佳方法。

<?php
$merchantid = 10101011;
$jsarray = array('merchantid' => $merchantid);
$jsonresult = json_encode($jsarray);
$curl = curl_init("http://exampleAPI/SelfAPI");
curl_setopt_array($curl, array(
CURLOPT_POST            =>  true,
CURLOPT_HEADER          =>  true,
CURLOPT_RETURNTRANSFER  =>  true,
CURLOPT_POSTFIELDS      => array( 'payload' => $jsonresult),
CURLOPT_HTTPHEADER      => array('Content-Type: application/json'),
CURLOPT_USERPWD         => "api_user:api_pass",
));
$result = curl_exec($curl);
$data = $result->Verify_lelong($merchantid);
curl_close($curl);
echo $result;
?>

我也因调用$ result-?Verify_lelong而出错,说:

Uncaught Error: Call to a member function Verify_lelong() on string

1 个答案:

答案 0 :(得分:0)

404错误表明找不到您正在使用的URL。检查您要传递给curl_init()的值是数据应发送到的URL。一旦正确,您就可以继续进行-祝您好运!