我想在普通的php中调用api,但是当我使用file_get_contents,CURL,get_header时,我已经使用我的web服务器进行了检查,启用了。(openssl,extension = php_openssl.dll)
我想知道为什么这不运行?
使用file_get_contents
<?php
$username = 'ram@demo.com';
$password = 'newnew';
$medium = 'WEB';
$json = file_get_contents("http://new.ezeeinfosolutions.com/busservices/auth/getAuthToken?namespaceCode=demo&username=ram@demo.com&password=newnew&devicemedium=WEB");
$array = json_decode($json, true);
echo $array;
echo $array['status']; // status
echo $array['datetime']; // date time
echo $array['data']['authToken']; // auth token
echo $array['data']['role']['code']; // code
?>
输出: file_get_contents(http://new.ezeeinfosolutions.com/busservices/auth/getAuthToken?namespaceCode=demo&username=ram%40demo.com&password=newnew&devicemedium=WEB):无法打开流:HTTP请求失败!第5行/home/vkacadem/public_html/api/index.php中不允许使用HTTP / 1.1 405方法。
使用crul:
$ch = curl_init("http://new.ezeeinfosolutions.com/busservices/auth/getAuthToken?namespaceCode=demo&username=$username&password=$password&devicemedium=$medium");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
**0/p: no response?**
我的API正在通过GET方法等待调用。
答案 0 :(得分:0)
您需要POST数据。添加
curl_setopt($ch, CURLOPT_POST);
发送请求之前。
建议:如果您使用的是Chrome,请安装Postman扩展程序。将允许您轻松调试此类问题。
答案 1 :(得分:0)
这些是您的API(http://new.ezeeinfosolutions.com/busservices/auth/getAuthToken?namespaceCode=demo&username=ram%40demo.com&password=newnew&devicemedium=WEB)返回的标头:
Response Headers
Allow:POST
Connection:keep-alive
Content-Length:0
Date:Mon, 27 Oct 2014 10:33:02 GMT
Server:nginx/1.4.4
此资源仅允许POST方法,因此您需要对此请求使用POST。