1

I wrote this code:

 $data = array(
    'user' => '*****',
    'password' => '****',
     'terminal' => '******',
      'GoodURL' => '********',
       'Total' => *******,
    );
    $jsonData = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://gateway20.pelecard.biz/PaymentGW/init");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;','Content-Length: ' . strlen($jsonData)));
        if ($call == 'pay') {
            $result = curl_exec($ch);
            $serverData = json_decode($result, true);
            echo $serverData['URL'];
        }

the parameters are good in the data array, but I always get result = false. can anyone told me what I am doing wrong?
Thanks

BhushanK
  • 1,205
  • 6
  • 23
  • 39
tal azouri
  • 43
  • 1
  • 7

1 Answers1

0

You shall implement error handling and cURL itself can tell you the issue. Using http://php.net/manual/en/function.curl-error.php, you can do:

print __LINE__;
if ($call == 'pay') {
   $err = curl_error($ch);
   print __LINE__;
   if (!empty($err)) {
     throw new Exception($err);
   }
   $result = curl_exec($ch);
   print serialize($result);
   $serverData = json_decode($result, true);
   print_r($serverDATA);
   echo $serverData['URL'];
}

And then you will be able to see the reason of the failure. Based on the other parts of the code, you might want to print it out, put it in a log database, and so on, throwing an exception is not always the proper way of handling that.

Aron Novak
  • 314
  • 1
  • 14
  • i tried to use your code,and i don't get any error. is that the corrcet way to send the request? – tal azouri Sep 10 '15 at 08:49
  • Generally yes, it seems to be a legit request. I updated the debug version, can you try it again? – Aron Novak Sep 10 '15 at 08:56
  • still get nothing, i see in http://php.net/manual/en/function.serialize.php, that serialize() not handle resource type. – tal azouri Sep 10 '15 at 09:10
  • That's interesting, `curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);` should prevent curl_exec() to return a resource. So let's try to examine your request with: `print_r(curl_getinfo($ch));` at the bottom of code snippet – Aron Novak Sep 10 '15 at 09:18
  • when i print the curl_getinfo($ch) i get:Array ( [url] => https://gateway20.pelecard.biz/PaymentGW/init [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.047 [namelookup_time] => 0 [connect_time] => 0.016 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) [redirect_url] => – tal azouri Sep 10 '15 at 09:33
  • Then try this: http://stackoverflow.com/questions/4570973/anything-wrong-with-my-curl-code-http-status-of-0 - it might be the SSL certificate of the HTTPS site. – Aron Novak Sep 10 '15 at 09:58
  • Ok, just to make it secure, not just "works for me", please follow http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/ - see the "proper fix" part – Aron Novak Sep 10 '15 at 12:23