2015-07-04 43 views
1

我有一个API,我从中检索统计信息并需要解析某些信息以在视图中返回。从Laravel的API返回消息解析一个值PHP

我已经写了一个curl请求来获取我需要从API中获取的信息。

// create curl resource 
     $ch = curl_init($cdnifybandwidthurl); 

    // sets GET 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); // -X  

    // $output contains the output string 
    $bandwidth_usage = curl_exec($ch); 

    // close curl resource to free up system resources 
    curl_close($ch); 

该请求将返回这样

{"resource":"xxxx","datefrom":"2015-07-01", 
"dateto":"2015-07-31","origin":"xxx.s3-website-us-east-1.amazonaws.com", 
"overall_usage":[[{"timestamp":"2015-07-02 
00:00:00","cached":1739969,"non_cached":340826,"hits":76}]]} 

我需要做的是返回视图中的non_cached量的字符串。我无法从字符串中提取这一数量。

回答

0

整体使用看起来像列表清单给我你必须决定,以防有更多的项目,你想要哪一个。

您的API返回JSON,所以我建议$obj = json_decode($bandwidth_usage, true),并从这里的列表中访问您需要的内容$obj['overall_usage']还是我错过了观点?

假设我真的轨道上,你可以在这种特殊情况下继续使用:

$obj['overall_usage'][0][0]["non_cached"] 
        ^first item of first array 
         ^first item of second array 
          ^item with key "non_cached" 
+0

你是对的,但在overall_usage我怎么访问non_cached值,并在视图退回呢? – TheDizzle

+0

@TheDizzle检查我的编辑是否有帮助 – enpenax