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

Php Curl exec返回的不仅仅是JSON。如何仅提取JSON数据?,php,json,curl,Php,Json,Curl,My curl exec响应返回以下内容: 字符串(151937)“HTTP/1.1 200确定访问控制允许标头:内容类型、接受租户、授权访问控制允许方法:发布、获取、放置、修补、选项访问控制允许来源:*缓存控制:私有内容类型:应用程序/json;charset=utf-8日期:2015年9月28日星期一08:35:33 GMT服务器:Microsoft IIS/7.5警告:不支持的身份验证方案X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET内容长度

My curl exec响应返回以下内容:

字符串(151937)“HTTP/1.1 200确定访问控制允许标头:内容类型、接受租户、授权访问控制允许方法:发布、获取、放置、修补、选项访问控制允许来源:*缓存控制:私有内容类型:应用程序/json;charset=utf-8日期:2015年9月28日星期一08:35:33 GMT服务器:Microsoft IIS/7.5警告:不支持的身份验证方案X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET内容长度:151475连接:保持活动{“ShortResultText”:“SE19”,“餐厅”:[{“Id”:50371,“姓名”:“Mahjestics加勒比美食”,“地址”:“247吉普赛路”,“邮政编码”:“SE27 9QY”,“城市”:“伦敦”,“菜系类型”:[{“Id”:76,“名称”:“加勒比海”,“SeoName”:null},{“Id”:97,“名称”:“非洲”,“SeoName”:null}],“Url”:http://majestic-caribbean-cuisine-west-norwood.just-

但是JSON在这里从{ShortResultText]开始:

我只需要从curl响应中获取JSON数据,但我不确定最好的方法是什么?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);
$url=”http://api-interview.test.com/restaurants?q=se19";
$ch=curl_init();
$api_头=数组(
'内容类型:文本/普通;字符集=utf-8',
“接受租户:英国”,
'接受语言:en GB',
'授权:基本VGVjaFRlc3RBUEk6dXNlcjI=',
'主持人:api-interview.test.com'
);
//打印(api标题);
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_头,FALSE);
curl_setopt($ch,CURLOPT_HTTPHEADER,$api_headers);
//回声“;
//$data=curl\u exec($ch);
//回声“;
回声“;
$data=curl\u exec($ch);
回声“;
//只是在这里测试
//$json=json_encode($data,true);
//$json1=json_decode($json,true);
//var_dump($json1);
//打印$json1[“餐厅];
//$json=json_encode($data,true);
//foreach($json['restaurants']作为$value){
//echo$value->postcode;
// }
卷曲关闭($ch);

您只需将
CURLOPT_HEADER
设置为false。

尝试以下操作:

或者,您也可以只操纵阵列:


我可以看看你的curl代码吗?我刚刚更新了我的帖子。你想得到什么?例如,Name、CusineTypes和ratingstars你的json是错误的。试着在这里测试一下。我认为问题是json是随标题一起返回的。因为如果我将其设置为false,则不会返回任何内容。啊,但问题是$json变量中没有json。我不知道如何将有效的JSON与标头分开。
$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);
$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);