Skip to content
Advertisement

php curl reponse not showing

php curl response not showing please help to fix. when I run this code I do not get any response from recharge.php file

<?php
// recharge url
    $recharge_url = "http://localhost/api_system/recharge.php";
    $key = "";
    $number = "9134322935";
    $operators = "ada";
    $amount = "asa";
    $FinalUrl = $recharge_url ."?key=".$key ."&number=" . 
        $number. "&op=" . $operators ."&amount=" . $amount;
    $durl = curl_init($FinalUrl);
    curl_setopt($durl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($durl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($durl, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($durl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($durl, CURLOPT_URL, $FinalUrl);
    curl_setopt($durl, CURLOPT_RETURNTRANSFER, true);
    $resps = curl_exec($durl);
    $res = json_decode($resps);
    curl_close($durl);
    echo "<pre>";
    print_r($res);
?>

here is recharge.php file

<?php 
    
    if(isset($_GET['key'])){
    
    }else{
        echo json_encode(['status'=>'false','data'=>'API Hit Limit Exceed','result'=>'not']);
    }
    
?>

Advertisement

Answer

try this code, hope it will help you— your current file —

<?php
$recharge_url = "http://localhost/test/recharge.php";
$key = "";
$number = "9134322935";
$operators = "ada";
$amount = "asa";
$FinalUrl = $recharge_url . "?key=" . $key . "&number=" . $number . "&op=" . $operators . "&amount="
        . $amount;
$durl = curl_init($FinalUrl);
curl_setopt($durl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($durl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($durl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($durl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($durl, CURLOPT_URL, $FinalUrl);
curl_setopt($durl, CURLOPT_RETURNTRANSFER, true);
$resps = curl_exec($durl);
$res = json_decode($resps);
curl_close($durl);
echo "<pre>";
print_r($res);
?>

recharge.php

<?php

if (isset($_GET['key']) && $_GET['key'] != "") {
    echo json_encode(['status' => 'true', 'data' => 'working', 'result' => 'not']);
} else {
    echo json_encode(['status' => 'false', 'data' => 'API Hit Limit Exceed', 'result' => 'not']);
}
?>

and also you should put return/die/exit after echo statement to avoid the code execution after json_encode

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement