[英]Posting to API with cURL
I'm trying to post data to an API with cURL. 我正在尝试使用cURL将数据发布到API。 If I do it via the playground of the API provider it works.
如果我通过API提供程序的游乐场进行操作,那么它将起作用。
But through my code something doesn't seem to work: 但是通过我的代码,似乎无法正常工作:
$headers = array();
$headers[] = 'Authorization: hmac ' .$websiteKey.':'.$hmac .':'.$nonce . ':'.$time;
$curl = curl_init('testcheckout.buckaroo.nl/json/Transaction');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl,CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
return $result;
if(curl_error($curl))
{
echo 'error:' . curl_error($c);
}
curl_close($curl);
I tried error handling with the curl_error
function but that doesn't seem to be working either. 我尝试使用
curl_error
函数进行错误处理,但这似乎也不起作用。 I never get an error, even if I make some obvious mistakes like purposely changing my API secret to something that doesn't make sense. 即使我犯了一些明显的错误,例如故意将我的API机密更改为没有意义的内容,我也永远不会出错。 How do I send the data.
如何发送数据。 How do I post to the API and why am I not getting any errors?
如何发布到API,为什么我没有收到任何错误?
Several notes: 几点注意事项:
return
statement should be after to check the errors. return
语句应在检查错误之后。 $result === false
. $result === false
。 $c
is undefined in curl_error()
. $c
在curl_error()
未定义。 http://
). http://
)。 CURLOPT_URL
is redundant, because already defined in curl_init()
. CURLOPT_URL
是多余的,因为已经在curl_init()
定义了。 Code: 码:
$headers = array();
$headers[] = 'Authorization: hmac ' .$websiteKey.':'.$hmac .':'.$nonce . ':'.$time;
$curl = curl_init('http://testcheckout.buckaroo.nl/json/Transaction');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl,CURLOPT_POSTFIELDS, $post);
// curl_setopt($curl, CURLOPT_URL, $url); // already defined in curl_init().
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
if($result === false)
{
echo 'error:' . curl_error($curl);
}
curl_close($curl);
return $result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.