Friday, May 27, 2022

[SOLVED] PHP cURL not working giving me only white screen

Issue

I am new to PHP cURL and REST API Below is my code and i am getting plain white page with no errors. Is I am doing it in a correct way? If Yes please guide me the correct way of doing this.

PHP CODE

    <?php
    if(isset($_POST['submit'])){
    $custMobile = $_POST['mobile_number'];
    $orderAmount = $_POST['order_amount'];
    $curl_post_data = array(
                "appId" => 'SOME ID',
                "secretKey" => 'xyz3cvb3nmLK54Jhg5654dtafnjmjjn35456657',
                "orderId" => 123456,
                "orderAmount" => $orderAmount,
                "customerPhone" => $custMobile,
                "returnUrl" => 'http://localhost/pgtest/thanks.php',
                );
        $service_url = 'MY API URL';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $service_url);
    curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
    curl_setopt($ch, CURLOPT_POSTFIELDS,$curl_post_data);   // post data
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $json = curl_exec($ch);
    curl_close ($ch);
    $obj = json_decode($json);

    echo $obj;
    }
    ?>

FRONT END

<form action="process.php" method="POST" role="form" >
            <legend>PAYEMENT GATEWAY CHECK</legend>

            <div class="form-group">
                <label for="">Order Amount</label>
                <input type="text" name="order_amount" class="form-control" id="" placeholder="Order Amount">
            </div>
            <div class="form-group">
                <label for="">Mobile Number</label>
                <input type="text" name="mobile_number" class="form-control" id="" placeholder="Mobile Number">
            </div>



            <input type="submit" class="btn btn-primary" name="submit" value="Pay Now">
        </form>

Please guide me for this. Some knowledge about REST API and CURL is helpful.


Solution

Please add the curl_error() method so you can find the what is the error in your reuest.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
curl_setopt($ch, CURLOPT_POSTFIELDS,$curl_post_data);   // post data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
$error = curl_error($ch);

echo '<pre>';print_r($error);die;

curl_close ($ch);

Relating to 'SSL certificate problem: unable to get local issuer certificate' error. Rather obviously this applies to the system sending the CURL request (and no the server receiving the request)

1) Download the latest cacert.pem from http://curl.haxx.se/ca/cacert.pem

2) Add the following line to php.ini (if this is shared hosting and you don't have access to php.ini then you could add this to .user.ini in public_html)

curl.cainfo=/path/to/downloaded/cacert.pem

3) By default the FastCGI process will parse new files every 300 seconds (if required you can change the frequency by adding a couple of files as suggested here https://ss88.uk/blog/fast-cgi-and-user-ini-files-the-new-htaccess/)



Answered By - Harsh Sanghani
Answer Checked By - Clifford M. (WPSolving Volunteer)