www.webdeveloper.com
Results 1 to 4 of 4

Thread: read json file (ajax + php curl)

Hybrid View

  1. #1
    Join Date
    Apr 2007
    Location
    tel aviv
    Posts
    122

    Unhappy read json file (ajax + php curl)

    hi,

    what am i doing wrong? tried several ways but each time i get a json.parse error.

    here's the code:

    Code:
    function curl_file_get_contents($url) {
    	$ch = curl_init();
    	$timeout = 5; 
    	curl_setopt ($ch, CURLOPT_URL, $url);
    	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    	$file_contents = curl_exec($ch);		//curl output to string
    	curl_close($ch);
    	
    	$jsonIterator = new RecursiveIteratorIterator (
    		new RecursiveArrayIterator(json_decode($file_contents, TRUE)),
    		RecursiveIteratorIterator::SELF_FIRST
    	);
    	
    	foreach ($jsonIterator as $key => $value) {
    		if(is_array($value)) {
    			echo "$key:\n";
    		}
    		else {
    			echo "$key => $value\n";
    		}
    	}
    
    }
    tried it also without the recursiveIterator, just a simple foreach, didn't help.

    the result of the echo is sent to:

    Code:
    $json = json_encode(curl_file_get_contents($jsonPath));
    echo $json;
    ..and then with ajax to the div.

    Any ideas?

  2. #2
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    Not without the error it is giving....

  3. #3
    Join Date
    Apr 2007
    Location
    tel aviv
    Posts
    122

    the error i get

    heres what i see in my console:

    <b>Fatal error</b>: Uncaught exception 'InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead'

    and its pointing at the following line of code:

    Code:
    new RecursiveArrayIterator(json_decode($file_contents, TRUE)),
    so my $file_contents is empty, but i dont understand why..my json file is valid (and not empty).

    So..any ideas?

  4. #4
    Join Date
    Apr 2007
    Location
    tel aviv
    Posts
    122

    resolved solved...

    curl doesnt know relative paths...of course.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles