【问题标题】:php curl reponse not showingphp curl响应未显示
【发布时间】:2021-08-25 04:45:13
【问题描述】:

php curl 响应未显示,请帮助修复。当我运行此代码时,我没有从charge.php 文件中得到任何响应

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

这是recharge.php文件

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

【问题讨论】:

  • $resps 变量的输出是什么?尝试做 var_dump($resps);就在curl_exec 行之后。

标签: php api curl


【解决方案1】:

试试这个代码,希望它对你有所帮助--- 你当前的文件--

<?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']);
}
?>

并且你应该在 echo 语句之后放置 return/die/exit 以避免在 json_encode 之后执行代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-15
    • 2012-11-02
    • 2020-04-16
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    相关资源
    最近更新 更多