【问题标题】:How to know status of file() requests after excution执行后如何知道 file() 请求的状态
【发布时间】:2026-01-15 22:45:01
【问题描述】:

我有这个 api

public function airtime()
  {
      $send = 
      file("https://www.example.com/APIQueryV1.aspUserID=CK123&
      APIKey=456&OrderID=789.............."); 

      if($send)
      {
          return $status->ORDER_COMPLETED;
      }

return $status->ORDER_COMPLETED;

}else return $status->ORDER_CANCELLED;

我也这样写代码

<?php
     if(isset(POST["aitime"]))  {

     $send file("https://www.example.com/APIQueryV1.aspUserID=CK123&
     APIKey=456&OrderID=789............."); 

     if($send)
     {
         return $status->ORDER_COMPLETED;
     }

     echo "SENT";

     }else  echo "NOT SENT";


?>

这是预期的返回 json

{"date":"17th-Mar- 2019","orderid":"798","statuscode":"200","status":"ORDER_COMPLETED","remark":"Success","mobilenetwork":"MTN","mobilenumber":"081xxxxxxxxxxx"

,"ordertype":"100 Credit","amountcharged":"95","walletbalance":"863210.1"}

如果状态返回 ORDER_COMPLETED,我想知道发送的回显,或者如果状态返回其他内容但未得到正确,我想知道发送的回声,请需要帮助。

【问题讨论】:

  • file() 不是正确使用的函数(它会在漂亮打印的 json 上中断)、curl 或 file_get_contents
  • 它工作..谢谢现在我想得到“状态”的值我有这个if($send) { Print_r($send); } } echo $status;但没有工作我有这个json作为输出{"status":"INVALID_CREDENTIALS"}

标签: php arrays json api file


【解决方案1】:
public function airtime()
{
    $send = 
    file("https://www.example.com/APIQueryV1.aspUserID=CK123&
    APIKey=456&OrderID=789.............."); 

    if($send) {
        return $status->ORDER_COMPLETED; //return back from this function
    }

    //The code will only reach here if not completed
    return $status->ORDER_CANCELLED;

}


$result = airtime();

//Prints out value of `$status->ORDER_COMPLETED` or `$status- 
//>ORDER_CANCELLED`
echo $result;

代码不会按原样工作,因为$status 没有在任何地方定义,但上面是根据真假考虑返回的方式($send

【讨论】: