PHP to PHP cURL with a dash of JSON

I am working on building a rudimentary service which uses cURL to access some JSON-formatted data. From a php script I use cURL to make a request to a URL which is just a PHP script that outputs a JSON string via json_encode()-ing of an array of associative arrays (from database results). Visiting the page in the browser shows that the output is working:

[{"label":"Fun Fact","bits":"Did you know that the name \\"Floppy Deuce\\" is actually based on a handshake? True Story."},{"label":"Did You Know","bits":"Floppy Deuce offers competitive pricing for printing services. Moral of the story: If you need 300 bobblehead dolls that look like Gary Busey, we got you covered."},{"label":"F2 History","bits":"Floppy Deuce Co-Founder Chris Smola was a high school mathematics teacher for four years before becoming a full-time web developer. His new job is way more fun."}]

However, printing the curl_exec() data shows that only the first object in the array of objects has been returned:


[{"label":"Fun Fact","bits":"Did you know that the name \\"Floppy Deuce\\" is actually based on a handshake? True Story."}] 

I have tried setting some of the cURL options as well as altering the cache and content-type headers in the output script, but nothing seems to work. I would prefer not to change to xml as it is more cumbersome to work with in PHP… =[

I’m sure I’m missing something blatantly obvious. Here is the curl code as it is now:


$handle = curl_init('http://f2.floppydeuce.com/feeds/f2bits');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($handle, CURLOPT_POST, 1);

//this creates an array with one element which is an object
//$bits = json_decode(curl_exec($handle));
$bits = curl_exec($handle);

curl_close($handle);

and these are the headers I tried (after trying the transaction with none) in the output script. I’ve heard maybe text/json or text/plain might be the way to go?


header('Content-type: application/json');            
header('Cache-Control: no-cache, must-revalidate');   

If your using a relatively modern version of PHP, you can try and do it without cURL and see what that shows up


$bits = file_get_contents('http://f2.floppydeuce.com/feeds/f2bits');
var_dump($bits)

Though going to that URL from here only shows 1 object in the array


[{"label":"Fun Fact","bits":"Did you know that the name \\"Floppy Deuce\\" is actually based on a handshake? True Story."}]

Here’s a cURL function I use for a simple XML file.

/**
 * load XML file using cURL from outside URL
 * if cURL is not available, this defaults to using simplexml_load_file()
 * @param string $url
 */
function simplexml_load_file_curl($url) {
    $xml="";

    if(in_array('curl', get_loaded_extensions())){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $xml = simplexml_load_string(curl_exec($ch));
        curl_close($ch);
    }
    else{
        $xml = simplexml_load_file($url);
    }

    return $xml;
}

Got the same result. Running 5.2.x =[

Force flow, are all of the functions within your function native to php?

To debug, look into tcp mon (https://tcpmon.dev.java.net/). You can either download the client or just use the web client. It will detail you the header exchange. Pos both the request and response headers so we can take a look. If you have issues getting tcp mon setup, just let me know and I’ll help trouble shoot.

This is embarassing. :goof:

I felt I should take the time to resolve the situation even though I feel like the BIGGEST idiot right now. :injured:

swallows pride

I just started testing more locally as opposed to uploading saves to the server every 10 seconds. So, naturally, I’ve been working with my local MySQL database…yea…the URL I was requesting data from was the server URL. I hadn’t synced the databases before testing.

Sorry everyone! Thanks though to SimonJ for pointing me to towards software with which the ensuing problems triggered the solution. :x

Yep. 5.1.3 and forward.