cURL PHP RESTful 服务始终返回 FALSE
我在将 json 对象发布到使用 REST 的 API 时遇到一些困难。我是使用 cURL 的新手,但我已经进行了全面搜索,试图找到问题的答案,但没有找到答案。我的 cURL 请求总是返回 false。我知道它甚至没有发布我的 json 对象,因为我仍然会从 url 得到响应。我的代码如下。
<?php
//API KEY = APIUsername
//API_CODE = APIPassword
$API_Key = "some API key";
$API_Code = "some API code";
$API_email = "[email protected]";
$API_password = "ohhello";
$url = "http://someURL.com/rest.svc/blah";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
header('Content-type: application/json');
$authData = "{\"APIUsername\":\"$API_Key\",\"APIPassword\":\"$API_Code\",\"EmailAddress\":\"$API_email\",\"Password\":\"$API_password\"}";
curl_setopt($ch, CURLOPT_POSTFIELDS, $authData);
//make the request
$result = curl_exec($ch);
$response = json_encode($result);
echo $response;
curl_close()
?>
$response 仅返回“false”
有什么想法吗?
I am having some difficulties POSTing a json object to an API that uses REST. I am new to using cURL, but I have searched all over to try to find an answer to my problem but have come up short. My cURL request is always returning false. I know it isn't even posting my json object because I would still get a response from the url. My code is below.
<?php
//API KEY = APIUsername
//API_CODE = APIPassword
$API_Key = "some API key";
$API_Code = "some API code";
$API_email = "[email protected]";
$API_password = "ohhello";
$url = "http://someURL.com/rest.svc/blah";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
header('Content-type: application/json');
$authData = "{\"APIUsername\":\"$API_Key\",\"APIPassword\":\"$API_Code\",\"EmailAddress\":\"$API_email\",\"Password\":\"$API_password\"}";
curl_setopt($ch, CURLOPT_POSTFIELDS, $authData);
//make the request
$result = curl_exec($ch);
$response = json_encode($result);
echo $response;
curl_close()
?>
The $response returns just "false"
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$response
可能为 false,因为curl_exec()
将 false(即失败)返回到$result
中。回显curl_error($ch)
(在调用curl_exec之后)以查看 cURL 错误(如果这是问题所在)。另一方面,我认为您的 CURLOPT_POSTFIELDS 的格式无效。您不向其传递 JSON 字符串。
更新
避免 SSL 错误的快速方法是添加此选项:
您会收到 SSL 验证错误,因为 cURL 与浏览器不同,没有预加载的受信任证书颁发机构 (CA) 列表,因此默认情况下不信任任何 SSL 证书。快速解决方案是使用上面的行仅接受证书而不进行验证。更好的解决方案是仅手动添加您想要接受的证书或 CA。请参阅此有关 cURL 和 SSL 的文章了解更多信息。
$response
is likely false becausecurl_exec()
returns false (i.e., failure) into$result
. Echo outcurl_error($ch)
(after the call to curl_exec) to see the cURL error, if that's the problem.On a different note, I think your CURLOPT_POSTFIELDS is in an invalid format. You don't pass a JSON string to that.
Update
The quick way to avoid the SSL error is to add this option:
You get an SSL verification error because cURL, unlike browsers, does not have a preloaded list of trusted certificate authorities (CAs), so no SSL certificates are trusted by default. The quick solution is to just accept certificates without verification by using the line above. The better solution is to manually add only the certificate(s) or CA(s) you want to accept. See this article on cURL and SSL for more information.