【发布时间】:2010-08-31 12:47:11
【问题描述】:
API 集成说明
API 需要使用一些输入字段和客户令牌将表单发布到 API URL。 API 处理然后将响应发布到我服务器上的 callback.php 文件。我可以使用该文件中的 $_POST 访问发布的 val。这就是现有方法的全部内容,并且效果很好。
要求
从客户端隐藏客户令牌值。所以我开始发送服务器端发布请求。
问题
我尝试了很多选项,但没有发生回调 -
1) CURL 方法
$ch = curl_init(API_URL);
$encoded = '';
$_postArray['customer_token'] = API_CUSTOMER_TOKEN;
foreach($_postArray as $name => $value)
{
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
// chop off last ampersand
$encoded = substr($encoded, 0, strlen($encoded)-1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
$resp = curl_exec($ch);
curl_close($ch);
echo $resp;
如果curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 行被删除但回调没有发生,则$resp 回显1。我正在回调脚本中设置会话变量来验证。是否需要 API 同步才能使用 curl 方法,以便 curl_exec 返回响应?
2) 没有 Posting parameters to a url using the POST method without using a form 中给出的 CURL
但是回调没有发生。
我也尝试使用以下代码,但看起来我的 pecl 安装不正确,因为未定义 HttpRequest()。
$req = new HttpRequest($apiUrl, HttpRequest::METH_POST);
$req->addQueryData($params);
try
{
$r->send();
if ($r->getResponseCode() == 200)
{
echo "success";
// success!
}
else
{
echo "failure";
// got to the API, the API returned perhaps a RESTful response code like 404
}
}
catch (HttpException $ex)
{
// couldn't get to the API (probably)
}
请帮帮我!我只需要轻松发送服务器端的 post 请求并在回调文件中获取响应。
【问题讨论】:
-
我猜问题出在其他地方。回调完全是 API 的责任,只要方法 1(cURL)返回
1(我猜这是预期的输出),问题就不在这里了。
标签: php api curl httprequest httpresponse