检索cURL返回的各个JSON数据

时间:2016-01-29 11:17:50

标签: php json curl

我使用cURL来获取我的请求的响应,给出的响应是JSON格式,一切正常,但我希望得到具体的 来自JSON响应的数据。当我回显整个数据时,它看起来像下面的

  

HTTP / 1.1 100继续HTTP / 1.1 200 OK服务器:nginx / 1.4.6(Ubuntu)日期:2016年1月29日星期五10:54:22 GMT内容类型:application / json; charset = utf-8内容 - 长度:379状态:200 OK访问控制允许来源:*设置Cookie:__bakery_session = BAh7BkkiD3Nlc3Npb25faWQGOgZFVEkDM2YWI0MWZm%0AZTBkZTEwYTBiOTBlZmU0MWFmYmU5ZTlhZmE5ZDE3NWFjNzg3ODY0MDYxZDgG%0AOwBG%0A - 0f1dadab3b89c8d34eb184b42b0781ce12c8adf0;路径= /; HttpOnly Set-Cookie:rack.session = BAh7BkkiD3Nlc3Npb25faWQGOgZFVEkiRWM0MWYwOWNiNTYyODM2YWI0MWZm%0AZTBkZTEwYTBiOTBlZmU0MWFmYmU5ZTlhZmE5ZDE3NWFjNzg3ODY0MDYxZDgG%0AOwBG%0A;路径= /; HttpOnly X-Served-By:bakery-breadroute-biscuit,bakery-prime-jubilee {“id”:17772310,“name”:“phpkAuyKh”,“type”:“Image”,“created”:“2016-01- 29T10:54:22 + 00:00" , “更新”: “2016-01-29T10:54:22 + 00:00”, “hashed_id”: “enekkvawvr”, “描述”: “”, “进步”: 0.0, “状态”: “排队”, “缩略图”:{ “URL”: “https://embed-ssl.wistia.com/deliveries/a70e7f7975ecb5f2bbf7b22e59f5d4d1112a4974.jpg?image_crop_resized=200x120”, “宽度”:200, “高度”:120}, “ACCOUNT_ID”:409257}

我的代码是:

<?php       

if (isset($_POST['submit']))
{
$url = "https://upload.wistia.com/?access_token=38316d1a944b503fc4408e0325daf2ceb3b58558238aab8d87c7cb6e2de2360b&project_id=8mtrjz3g8l";
$filename = $_FILES['file']['name'];
$filedata = $_FILES['file']['tmp_name'];
//$filesize = $_FILES['photo']['size'];
if ($filedata != '')
{
$headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading
$postfields = array("filedata" => "@$filedata", "filename" => $filename);
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postfields,
//CURLOPT_INFILESIZE => $filesize,
CURLOPT_RETURNTRANSFER => true
); // cURL options
curl_setopt_array($ch, $options);
$result=curl_exec ($ch);

echo $result."<br />";
echo '<br><br><br>+++++++++++++++++++++++++++++++++++++++++++++++++<br>';
//echo json_decode($result, TRUE);

//echo curl_error($ch);

curl_close ($ch);

}

}
?>

<!DOCTYPE html>
<html>
<head>
<title>Livehacks.tv</title>
<META CHARSET="UTF-8">
<META lang="en">
<META http-equiv="X-UA-Compatible" content="IE=edge">
<META name="viewport" content="width=device-width, initial-scale=1">            
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>   
<div class="left_signup_container">
<form name="wistia" method="post" action="<?php echo $PHP_SELF;?>" enctype="multipart/form-data" id="videoForm">
<input type="file" name="file" id="file">
<input type="submit" class="btn btn-success" name="submit" id="submit" value="upload">
</form>
</div> 
</body>
</html>

它会打印响应,但我尝试使用以下代码从它返回的数据中仅检索ID,但不能正常工作

json_decode($result, TRUE);
$id = $result->id;
echo $id;

有关如何在JSON数组中检索单个数据的任何建议.. !!

1 个答案:

答案 0 :(得分:1)

由于您返回的数据似乎不仅仅是一个JSON字符串,请修改您的代码以添加一些错误检查。

此外,您正尝试使用对象表示法来解决id,这很好,所以不要费心使用,TRUE参数将JSON字符串转换为数组。

json_decode($result);

if ( json_last_error() > 0 ) {
   echo json_last_error_msg();
   exit;
}

echo $result->id;