Ankit Sharma Ankit Sharma - 7 months ago 51
PHP Question

PHP cURL not working giving me only white screen

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.

Answer

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);