0

Hi everyone i have crul request when i use it in single php page without laravel it work fine but if i put it inside laraevl controller to use it not working and return code status 0

My Request

$data = '{"PaymentInquiryV4RequestMessage":{"RefNum":"123456"}}';                                                               
                                                                                     
                $ch = curl_init('https://b2btest.stcpay.com.sa/B2B.DirectPayment.WebApi/DirectPayment/V4/PaymentInquiry');                                                                      
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                                  
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
                curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
                curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1);
                curl_setopt($ch, CURLOPT_SSLCERT, 'crt.crt');
                curl_setopt($ch, CURLOPT_SSLKEY, 'key.key');            
                curl_getinfo($ch, CURLINFO_HTTP_CODE);
                                                       
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                    'Content-Type: application/json',   
                    'X-ClientCode: XXXXXXXXXXX'
                    )                                                                       
                );                                                                                                                   
                                                                                                                                     
                $result = curl_exec($ch);
                $result = json_decode($result, true);
                print_r ($result);

9
  • Not sure what that issue could be but on a side note, if you're using this in a laravel app, I would consider using the Guzzle client instead of using curl directly, see the docs Commented Oct 14, 2020 at 9:41
  • You might try running print_r ($result); before the json_decode, it could be that you are getting a response but its not valid JSON or there's something in the JSON causing a parsing error. Commented Oct 14, 2020 at 9:46
  • @WesleySmith Same error Commented Oct 14, 2020 at 9:47
  • try to catch error if (curl_errno($ch)) { $error_msg = curl_error($ch); } Commented Oct 14, 2020 at 9:47
  • @user969068 Nothing returned Commented Oct 14, 2020 at 9:48

1 Answer 1

1

when dealing with curl always start by debugging errors, which you can enable like

$result = curl_exec($ch); 
if (curl_errno($ch)) {     
   $error_msg = curl_error($ch); 
   echo $error_msg; 
} 

and as you mentioned in comment, you received below error after adding above code:

could not load PEM client certificate, OpenSSL error error:02001002:system library:fopen:No such file or directory, (no key found, wrong pass phrase, or wrong file format?) 

It clearly indicate it can't find your certificate files, update path to those files in your call , like if your certificates are in directory /var/www/certificate/, your code would be

        curl_setopt($ch, CURLOPT_SSLCERT, '/var/www/certificate/crt.crt');
        curl_setopt($ch, CURLOPT_SSLKEY, '/var/www/certificate/key.key'); 

and make sure cert files are readable by setting proper permissions.

p.s you can also use Laravel helpers to get the path

base_path();    // '/var/www/mysite'
app_path();     // '/var/www/mysite/app'
storage_path(); // '/var/www/mysite/storage'

so you call will contain like

    curl_setopt($ch, CURLOPT_SSLCERT, base_path().'/certificate/crt.crt');
    curl_setopt($ch, CURLOPT_SSLKEY, base_path().'/certificate/key.key'); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.