【发布时间】:2011-08-05 04:04:16
【问题描述】:
我在将 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@email.com";
$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”
有什么想法吗?
【问题讨论】:
标签: php json rest curl restful-authentication