1

I'm trying to post data to an API with cURL. If I do it via the playground of the API provider it works.

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. 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. How do I send the data. How do I post to the API and why am I not getting any errors?

Syscall
  • 19,327
  • 10
  • 37
  • 52
Kevin.a
  • 4,094
  • 8
  • 46
  • 82
  • 2
    Your `return` statement stops the execution of your function, so, `curl_error()` is never called. Try to move it under `curl_close()`. – Syscall May 18 '18 at 09:37
  • see if this is not the cause: https://stackoverflow.com/questions/4372710/php-curl-https – Eriks Klotins May 18 '18 at 09:38
  • @Syscall i did that now, still no errors even if after i changed my api secret to something that doesnt make sense – Kevin.a May 18 '18 at 09:41
  • Try `if($result === false)` instead of `if(curl_error($curl))`. Or, what is the output of `var_dump($result)`? – Syscall May 18 '18 at 09:45
  • @Syscall var_dump returns nothing hmm. And Still no error even after changing my way to yours. – Kevin.a May 18 '18 at 09:51
  • `var_dump()` never returns nothing. Add a `die('stop');` after `var_dump`. Also, use `curl_error($curl)` instead of `curl_error($c)`. – Syscall May 18 '18 at 09:55
  • Another thing, what is the value of `$url`? It's redundant with the argument of `curl_init()`. – Syscall May 18 '18 at 09:57
  • @Syscall i did it. The var_dump returns: string(0) "". The argument in curl_init is the same as the $url variable – Kevin.a May 18 '18 at 10:03

1 Answers1

2

Several notes:

  • The return statement should be after to check the errors.
  • To check errors, you should test $result === false.
  • The variable $c is undefined in curl_error().
  • The URL is mal-formed, missing the scheme (eg, http://).
  • The option CURLOPT_URL is redundant, because already defined in 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;
Syscall
  • 19,327
  • 10
  • 37
  • 52