我试图从以下JSON获取结果部分中的indications_and_usage部分:https://api.fda.gov/drug/label.json?search=levodopa
到现在为止,我
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$data = curl_exec($ch);
curl_close($ch);在我的php文件中。当我回显$data时,它会给出整个json作为输出。我如何修改它以获得indications_and_usage部件?
由于所有方法似乎都不起作用,下面是我开始输出的内容。
echo $data;HTTP/1.1 200连接建立了HTTP/1.1 200确定访问-控制-允许-头:X-请求-与访问-控制-允许-起源:*年龄:0缓存-控制:公共,最大年龄=60内容-安全-策略:默认-src‘无’内容-类型:应用程序/json;charset=utf-8 Date: Sun,2016年2月21日19:49:27 GMT ETag: W/"19923-bQuoDHROKCsX/qDsyE4GuA“服务器: openresty :接受-编码通过: http/1.1 api-伞(ApacheTrafficServer cSsSfU) X-缓存:小姐X内容-类型-选项: nosniff X-框架-选项:拒绝X-XSS-保护: 1;mode=block内容-长度: 104739连接:保持-存活{ "meta":{”免责声明“:"openFDA是一个测试版研究项目,不供临床使用。虽然我们尽一切努力确保数据是准确的,但您应该假定所有结果都未经验证。“、”许可“:"http://open.fda.gov/license”、"last_updated":"2016-02-05“、”结果“:{”跳过“:0、”限制“:1、”总计“:1400 }、”结果“:[{ "effective_time":"20120305","drug_interactions":[药物相互作用:很少收集与其他药物同时使用的安非他酮代谢的系统数据,或者同时服用安非他酮对其他药物代谢的影响。.诸若此类
发布于 2016-02-21 19:39:43
您必须删除curl_setopt($ch,CURLOPT_HEADER,1);形成卷曲。否则,将在变量$data中包含http标头。
完整的示例代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
//curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$result = json_decode($data, true);
$result_string = $result['results'][0]['indications_and_usage'][0];
echo $result_string;发布于 2016-02-21 19:43:11
$json = file_get_contents("https://api.fda.gov/drug/label.json?search=levodopa");
$obj = json_decode($json);
echo "<pre>";
print_r($obj->results[0]->indications_and_usage);
echo "</pre>";如果对整个对象执行print_r或var_dump操作,您将看到results是其成员之一,一个数组。结果数组的第一个索引是另一个对象,它有一个indications_and_usage成员,是您想要的。
发布于 2016-02-21 19:37:09
您应该能够通过以下方式访问它:
$decoded = json_decode($data, true);
echo $decoded->results[0]->indications_and_usage[0]
https://stackoverflow.com/questions/35540963
复制相似问题