我在PHP yii2中得到数组(0)?

问题描述 投票:0回答:1

我得到array(0)作为var_dump。

tbl_supplierOrderDetails['user_id']array(2) {
  ["tbl_supplierOrderDetails"]=>
  array(0) {
  }
  ["tbl_userContactDetails"]=>
  array(0) {
  }
}

这是我使用php yii2的API示例

 public function actionSuggestions()
    {   
        $listingId = Yii::$app->request->get('listing_id');
        $apiUrl = "http://example.com:8800/api/Home/view_supplierOrderDetails?listing_id=".$listingId;
        $suggestions = file_get_contents($apiUrl);
        $suggestions = json_decode($suggestions, JSON_PRETTY_PRINT);
        echo "tbl_supplierOrderDetails['user_id']";
        //print_r($suggestions);
        var_dump($suggestions);
         exit;
        return $this->render('suggestions' , array(
            'listingId' => $listingId,
            'suggestions' => $suggestions,
        ));
    }

这是我的json格式的api演示。

 {
      "tbl_supplierOrderDetails": [
        {
          "num": 1,
          "user_id": 3496,
          "priority": "A.closed",
          "no_of_times": 5,
          "username": "eyuw",
          "company_name": "djff",
          "email": "[email protected]",
          "contact_no": "9999999999",
          "origin": "abc",
          "dest": "xyz",
          "vehicle_class": "abc,
          "vehicle_type": "abc",
          "notes": "TCS_"
        }]
    "tbl_userContactDetails": [
        {
          "user_id": 500,
          "contact_name": "Mr abc",
          "contact_email": "[email protected]",
          "contact_mobile": 9999999999,
          "contact_mobile2": null
        }]
    }

我想在不同部分的html中显示这些细节。我怎么能通过调用这个API来这样做。

php jquery json yii2 yii2-advanced-app
1个回答
0
投票

首先,确保变量$ suggestions包含您期望的数据。

你的结构是完全错误的,所以我想建议你一个好的结构。

public function actionSuggestions() {
    try {
        $message = array();
        $listingId = Yii::$app->request->get('listing_id');
        if (!empty($listingId)) {
            $apiUrl = "http://example.com:8800/api/Home/view_supplierOrderDetails?listing_id=" . $listingId;
            $suggestions = file_get_contents($apiUrl);
            $suggestions = json_decode($suggestions, JSON_PRETTY_PRINT);

            //if $suggestions contains required data then you can return those data
            $message = ['status' => 'success', 'message' => 'Data fetched succesfully', 'data' => $suggestions];

            //else build an array like what format you want
        } else {
            $message = ['status' => 'failed', 'message' => 'Missing parameters.'];
        }
    } catch (\Exception $exception) {
        $message = ['status' => 'failed', 'message' => "Some exception occured."];
    }
    return $message;
}

在这里你可以期待json输出如下:

{
"status" : "success",
"message" : "Data fetched succesfully",
"data" : {

    }
 }
© www.soinside.com 2019 - 2024. All rights reserved.