cURL PHP RESTful 服务始终返回 FALSE

发布于 2024-11-01 00:24:51 字数 1049 浏览 1 评论 0原文

我在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

计㈡愣 2024-11-08 00:24:51

$response 可能为 false,因为 curl_exec() 将 false(即失败)返回到 $result 中。回显 curl_error($ch) (在调用curl_exec之后)以查看 cURL 错误(如果这是问题所在)。

另一方面,我认为您的 CURLOPT_POSTFIELDS 的格式无效。您不向其传递 JSON 字符串。

该参数可以作为
urlencoded 字符串,例如
'para1=val1¶2=val2&...' 或作为
以字段名称为键的数组,
字段数据作为值。

-- curl_setopt() 的 PHP 文档


更新

避免 SSL 错误的快速方法是添加此选项:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

您会收到 SSL 验证错误,因为 cURL 与浏览器不同,没有预加载的受信任证书颁发机构 (CA) 列表,因此默认情况下不信任任何 SSL 证书。快速解决方案是使用上面的行仅接受证书而不进行验证。更好的解决方案是仅手动添加您想要接受的证书或 CA。请参阅此有关 cURL 和 SSL 的文章了解更多信息。

$response is likely false because curl_exec() returns false (i.e., failure) into $result. Echo out curl_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.

This parameter can either be passed as
a urlencoded string like
'para1=val1¶2=val2&...' or as an
array with the field name as key and
field data as value.

-- PHP docs for curl_setopt()


Update

The quick way to avoid the SSL error is to add this option:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文