0

I am trying to verify a payment transaction on Paystack using PHP/cURL Although the "catch-error" block of code indicates there is error, echo curl_error($ch) does not output any error message. Below is code I have used:

            $url = " https://api.paystack.co/transaction/verify/" . urlencode($reference);
            
            
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //remove in production

            curl_setopt($ch,CURLOPT_URL, $url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
            
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "accept: application/json",
            "Authorization: Bearer sk_test_2413efe85fd9d8f19cdb888f9568ee4ce7326543",
            "Cache-Control: no-cache"
            ));

            $sResult = curl_exec($ch);
        
            
            
            if(curl_errno($ch)){
                echo 'Error: ' . curl_error($ch);
                
            }
            else{
                echo $sResult;
           }

From the code above, I simply get Error: as output from this block of code:

if(curl_errno($ch)){
                
    echo 'Error: ' . curl_error($ch);
                
}

print_r(curl_getinfo($ch)); prints the following:

Array

(

[url] =>  https://api.paystack.co/transaction/verify/106020220117133308
[content_type] => 
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 0
[connect_time] => 0
[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
[redirect_url] => 
[primary_ip] => 
[certinfo] => Array
    (
    )

[primary_port] => 0
[local_ip] => 
[local_port] => 0
[http_version] => 0
[protocol] => 0
[ssl_verifyresult] => 0
[scheme] => 
[appconnect_time_us] => 0
[connect_time_us] => 0
[namelookup_time_us] => 0
[pretransfer_time_us] => 0
[redirect_time_us] => 0
[starttransfer_time_us] => 0
[total_time_us] => 0
)

What code be solution to this problem. I have searched similar problems on SO but it appears answer to each problem is specific to the problem.

  • If you are not getting a valid HTTP response (a 200 response, or some other valid HTTP response code like 500) then it is not connecting to the service. See here for valid responses: https://www.php.net/manual/en/function.http-response-code.php – Shaun Bebbers Jan 17 '22 at 14:15
  • You have a space before the `https` in the `$url` which I think is breaking it. – cOle2 Jan 17 '22 at 14:24
  • What code be some of the reasons why it may not be connecting to the service? – programmer777 Jan 17 '22 at 14:24
  • @cOle2 you solved my problem! The problem was solved after I deleted the space. Thank you – programmer777 Jan 17 '22 at 14:27

0 Answers0