1

I am trying to get the indications_and_usage part in the results section from the following JSON URL: https://api.fda.gov/drug/label.json?search=levodopa

Till now, I have

$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);

in my php file. When I echo $data, it gives the entire json as output. How can I modify this to get the indications_and_usage part?

As none of the methods seem to be working, here is the beginning output when I do

echo $data;

HTTP/1.1 200 Connection established HTTP/1.1 200 OK Access-Control-Allow-Headers: X-Requested-With Access-Control-Allow-Origin: * Age: 0 Cache-Control: public, max-age=60 Content-Security-Policy: default-src 'none' Content-Type: application/json; charset=utf-8 Date: Sun, 21 Feb 2016 19:49:27 GMT ETag: W/"19923-bQuoDHROKCsX/qDsyE4GuA" Server: openresty Vary: Accept-Encoding Vary: Accept-Encoding Vary: Accept-Encoding Via: http/1.1 api-umbrella (ApacheTrafficServer [cSsSfU]) X-Cache: MISS X-Content-Type-Options: nosniff X-Frame-Options: deny X-XSS-Protection: 1; mode=block Content-Length: 104739 Connection: keep-alive { "meta": { "disclaimer": "openFDA is a beta research project and not for clinical use. While we make every effort to ensure that data is accurate, you should assume all results are unvalidated.", "license": "http://open.fda.gov/license", "last_updated": "2016-02-05", "results": { "skip": 0, "limit": 1, "total": 1400 } }, "results": [ { "effective_time": "20120305", "drug_interactions": [ "DRUG INTERACTIONS Few systemic data have been collected on the metabolism of bupropion following concomitant administration with other drugs or, alternatively, the effect of concomitant administration of bupropion on the metabolism of other drugs. ..... and so on

Mahatma Gandhi
  • 41
  • 1
  • 1
  • 6

3 Answers3

1

You have to remove curl_setopt($ch, CURLOPT_HEADER, 1); form your curl. Otherwise the http header is included in the variable $data.

Full Example Code:

$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;
1
$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>";

If you do print_r or var_dump on the whole object, you'll see that results is one of its members, an array. The first index of the results array is another object, which has an indications_and_usage member, the one you want.

geco17
  • 5,152
  • 3
  • 21
  • 38
0

You should be able to access it with:

$decoded = json_decode($data, true);

echo $decoded->results[0]->indications_and_usage[0]

Matt
  • 5,315
  • 1
  • 30
  • 57
  • Not working. I added $info = $data['results']['indications_and_usage']; echo $info; But it is giving an error: Illegal string offset 'results' – Mahatma Gandhi Feb 21 '16 at 19:39
  • 1
    Why do people insist on converting perfectly lovely objects into arrays? – RiggsFolly Feb 21 '16 at 19:48
  • 1
    @PaulCrovella Ah, So you are talking about badly designed JSON data structures. Hmmm bit like badly designed XML and HTML etc etc – RiggsFolly Feb 21 '16 at 19:58