Curl exec返回的不仅仅是JSON。如何仅提取JSON数据?

时间:2015-09-28 08:43:22

标签: php json curl

我的curl exec响应返回:

string(151937) "HTTP/1.1 200 OK Access-Control-Allow-Headers: Content-Type, Accept-Tenant, Authorization Access-Control-Allow-Methods: POST,GET,PUT,PATCH,OPTIONS Access-Control-Allow-Origin: * Cache-Control: private Content-Type: application/json; charset=utf-8 Date: Mon, 28 Sep 2015 08:35:33 GMT Server: Microsoft-IIS/7.5 Warning: Unsupported Authentication Scheme X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 151475 Connection: keep-alive {"ShortResultText":"SE19","Restaurants":[{"Id":50371,"Name":"Mahjestics Caribbean Cuisine","Address":"247 Gypsy Road","Postcode":"SE27 9QY","City":"London","CuisineTypes":[{"Id":76,"Name":"Caribbean","SeoName":null},{"Id":97,"Name":"African","SeoName":null}],"Url":"http://majestic-caribbean-cuisine-west-norwood.just- ...

但是JSON从“{ShortResultText”开始......:

{
    "ShortResultText": "SE19",
    "Restaurants": [
        {
            "Id": 50371,
            "Name": "Mahjestics Caribbean Cuisine",
            "Address": "247 Gypsy Road",
            "Postcode": "SE27 9QY",
            "City": "London",
            "CuisineTypes": [
                {
                    "Id": 76,
                    "Name": "Caribbean",
                    "SeoName": null
                },
                {
                    "Id": 97,
                    "Name": "African",
                    "SeoName": null
                }
"Url": "http://majestic-caribbean-cuisine-west-norwood.test.co.uk",
            "IsOpenNow": true,
            "IsSponsored": false,
            "IsNew": false,
            "IsTemporarilyOffline": false,
            "ReasonWhyTemporarilyOffline": "",
            "UniqueName": "majestic-caribbean-cuisine-west-norwood",
            "IsCloseBy": false,
            "IsHalal": true,
            "DefaultDisplayRank": 1,
            "IsOpenNowForDelivery": true,
            "IsOpenNowForCollection": true,
            "RatingStars": 4.71,
            "Logo": [
                {
                    "StandardResolutionURL": "http://d30v2pzvrfyzpo.cloudfront.net/uk/images/restaurants/50371.gif"
                }
            ],
            "Deals": [],
            "NumberOfRatings": 7

我需要从我的curl响应得到JUST数据,我不确定最好的方法吗? curl响应头的长度可能会有所不同,具体取决于POST的“ShortResultText”值,因为这是一个变量。

然后我将能够在数组中获取数据并循环遍历它。

卷曲代码:

$url = "http://api-interview.test.com/restaurants?q=se19";
$ch = curl_init();

$api_headers = array(
    'Content-Type: text/plain; charset=utf-8',
    'Accept-Tenant: uk',
    'Accept-Language: en-GB',
    'Authorization: Basic VGVjaFRlc3RBUEk6dXNlcjI=',
    'Host: api-interview.test.com'
    );

//print_r($api_headers);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $api_headers);


// echo "<pre>";
// $data = curl_exec($ch);
// echo "</pre>";

echo "<pre>";

$data = curl_exec($ch);

echo "</pre>";

//just testing here
//    $json = json_encode($data, true);
//    $json1 = json_decode($json, true);
//    var_dump($json1);

//print $json1['restaurants'];

//$json = json_encode($data, true);

// foreach($json['restaurants'] as $value) {
//  echo $value->postcode;
// }
curl_close($ch);

2 个答案:

答案 0 :(得分:0)

您只需将CURLOPT_HEADER设置为false。

答案 1 :(得分:0)

试试这个:

$json1 = json_decode($json, true);

foreach($json1['Restaurants'] as $key => $val){
 print_r($val); // You will get here the ID, Name ....

  echo 'ID: ' $val['Id'] . 'Name: ' . $val['Name']; . 'Rating Stars: ' . $val['RatingStars']..

   //If you want to go deeper and get the CuisineTypes just do below 
   foreach($val['CuisineTypes'] as $key2 => $val2) {
     print_r($val2); //This is the cuisine types

   }
}

或者你可以操纵你的阵列:

$newData = [];

foreach($json1['Restaurants'] as $key => $val)
{
  $newData[] = ['Name' => $val['Name'], 'CusineTypes' => $val['CuisineTypes'], 'RatingStars' => $val['RatingStars']];
}
  print_r($newData);