【问题标题】:retrieve individual JSON data returned by cURL检索 cURL 返回的单个 JSON 数据
【发布时间】:2016-05-07 02:38:38
【问题描述】:

我使用 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访问控制允许来源:*的Set-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","updated":"2016-01-29T10:54:22+00:00","hashed_id":"enekkvawvr","description":"","progress": 0.0,"status":"queued","thumbnail":{"url":"https://embed-ssl.wistia.com/deliveries/a70e7f7975ecb5f2bbf7b22e59f5d4d1112a4974.jpg?image_crop_resized=200x120","width":200,"height":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 数组中检索单个数据的任何建议..!!

【问题讨论】:

  • $response = json_decode($result, TRUE); $data['id'] = 数组(); $data['id'] = $response['id'];回声 $data['id'];
  • 我试过你的解决方案并回显 $data['id'] ,但它不显示 id
  • 您的$result 不仅仅是一个 JSON 字符串,因此不是有效的 JSON 字符串
  • 对不起,我也犯了一个错误。将像这样工作: $response = json_decode(@file_get_contents($urlForJSON), TRUE); $data['id] = array(); $data['id'] = $response['id'];回声 $data['id'];在使用您的 JSON 进行测试时,我确实从 echo 中获得了正确的 id。但请记住,我必须修改 JSON 删除它的标题部分,因为它不能与它一起使用。
  • 我应该替换 $urlForJSON..???

标签: php json curl


【解决方案1】:

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

您还尝试使用对象表示法来处理id,这很好,所以不要费心使用,TRUE 参数将您的JSON 字符串转换为数组。

json_decode($result);

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

echo $result->id;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2014-09-16
    • 2012-12-24
    • 2019-08-01
    相关资源
    最近更新 更多