user1330226
user1330226

Reputation: 1

Removing "Array ()" from CURL result in PHP

I'm trying to fetch data from a password protected JSON feed using curl, but the result adds "Array (" to the beginning of the feed and ")" at the end, making it invalid.

I'm using this code:

<?php
  $url = 'https://slx.arlcap.com/sdata/rcs/tablet/products/Y6UJ9A00000Z/filings';
  $username = 'xxx';
  $password = 'xxx';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
  curl_setopt($ch, CURLOPT_HEADER, false);
  //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  //curl_setopt($ch, CURLOPT_REFERER, $url);
  $result = curl_exec($ch);
  $info = curl_getinfo($ch);
  curl_close($ch);
  $data = json_decode($result, true);
  print_r($data);
?>

And the result is here: http://www.motion.tc//DataTables-1.9.0/examples/ajax/feed.php

Is there a way I can have the data return without the "Array()" element being added?

Thanks! Andrew

Upvotes: 0

Views: 492

Answers (2)

Baba
Baba

Reputation: 95101

You getting such format because you are trying to print an array http://php.net/manual/en/language.types.array.php if you are developing service based application I think you use use a standard format such as json for your output ....

Please See :

http://www.php.net/manual/en/function.json-decode.php

http://www.php.net/manual/en/function.json-encode.php

It would give you a much cleaner code with better compatibility with other technology such as javascript etc.

Upvotes: 0

hjpotter92
hjpotter92

Reputation: 80639

That is because you're using a print_r function. To get only the values inside of the variable, do an echo with a foreach loop. Something like this:

foreach( $data as $key => $val ) {
  echo $key . " -> " . $val;
}

If it isn't a nested array. If it is nested array, search SO for printing them effectively.

Upvotes: 2

Related Questions