使用PHP Curl发布数据后检索响应
我正在尝试使用Curl对本地比特币API进行API调用,但很难正确返回响应。我已经看过我的卷发要求,没有什么不合适的 下面是我试图获得的API调用: 基本URL: 所需参数:使用PHP Curl发布数据后检索响应,php,post,curl,oauth-2.0,response,Php,Post,Curl,Oauth 2.0,Response,我正在尝试使用Curl对本地比特币API进行API调用,但很难正确返回响应。我已经看过我的卷发要求,没有什么不合适的 下面是我试图获得的API调用: 基本URL: 所需参数:客户端id,客户端密码,用户名,密码,授权类型=密码 可选参数:无 如果成功,将立即返回JSON-like-so: { "access_token": the access token, "scope": "read", "expires_in": seconds to expiry, "refresh_to
客户端id
,客户端密码
,用户名
,密码
,授权类型=密码
可选参数:无
如果成功,将立即返回JSON-like-so:
{
"access_token": the access token,
"scope": "read",
"expires_in": seconds to expiry,
"refresh_token": a refresh token
}
下面是我编写的函数,用于尝试检索access\u令牌
:
function authenticate($parameters) {
$url = 'https://localbitcoins.com/oauth2/access_token/';
$parameters['client_id'] = 'redacted';
$parameters['client_secret'] = 'redacted';
$data = http_build_query($parameters);
// Initialize the PHP curl agent
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "curl");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
if ($result === false)
throw new Exception ("curl Error: " . curl_error($ch));
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_status != 200)
throw new Exception("Request Failed. http status: " . $http_status);
curl_close($ch);
// Trim any whitespace from the front and end of the string and decode it
$result = json_decode(trim($result));
if (($error = get_json_error()) !== false) {
throw new Exception("json_decode failed: " . $error);
}
// Verify that we got a valid response
if (!isset($result->status))
{
throw new Exception("API response did not contain 'status'");
exit;
}
if ($result->status == 'error')
{
throw new Exception("API call failed: " . $result->message);
exit;
}
// The API call succeeded, return the requested data
return $result->data;
}
并调用该函数:
authenticate(array(
'grant_type' => 'password',
'username' => 'redacted',
'password' => 'redacted'));
所有这些返回的都是致命错误,对于错误请求返回400错误。任何帮助都将不胜感激 你能试试这个吗
$result = curl_exec($ch);
echo nl2br($result);
curl_close($ch);
我认为为当前的本地比特币API开发是不值得的。它的文档记录非常糟糕,有很多不完整的功能。如果你看看开发者论坛,它没有得到很好的维护,而且充满了抱怨: 我个人向一位本地比特币开发人员询问了API的状态,他告诉我,他们将在未来两周内推出更新的API 具体地说,从你的问题来看,这个问题似乎从2013年10月就存在了: 看起来本地比特币团队也没有人解决这个问题。我认为你最好还是推迟他们的新API
干杯 没有任何结果。顺便说一下,这是一个控制台应用程序。我使用了print\r($result),只返回了
1
$ch=curl_init($URL)///要发送curl_setopt的URL($ch,CURLOPT_MUTE,1);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);卷曲设置($ch,卷曲设置桩,1);curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:text/xml'))//您的头类型curl\u setopt($ch,CURLOPT\u POSTFIELDS,“[您的XML内容]”);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);$output=curl\u exec($ch);echo nl2br(输出);卷曲关闭($ch);一旦你得到了回复,只需解析数据并放入你的变量…希望它对你有效,对我有效。我会给你一个机会并报告回来。谢谢你的帮助!在CURLOPT_HTTPHEADER中设置文件头您的“内容类型”是什么:由于API URL位于HTTPS上,您必须通过将CURLOPT_SSL_VERIFYPEER
选项设置为false
来禁用SSL对等验证。似乎就是这样。。。我将尝试更多地解决这个问题,但这似乎是可能的答案。谢谢