0

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

2
  • 2
    I see the oage as working, what did you expect to see? You are printing the parsed array, that was parsed correctly from the json Commented Apr 12, 2012 at 21:21
  • The only reason it displays with Array() wrapping it is that you have print_r()ed it. What format do you want it to be in? Commented Apr 12, 2012 at 21:23

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.