SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict Smola's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    [{"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:

    Code:
    [{"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:

    PHP Code:
    $handle curl_init('http://f2.floppydeuce.com/feeds/f2bits');
    curl_setopt($handleCURLOPT_RETURNTRANSFER1);
    curl_setopt($handleCURLOPT_FRESH_CONNECT1);
    curl_setopt($handleCURLOPT_POST1);

    //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?

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

    Smola

  2. #2
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your using a relatively modern version of PHP, you can try and do it without cURL and see what that shows up

    PHP Code:
    $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

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

  3. #3
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Here's a cURL function I use for a simple XML file.

    Code:
    /**
     * 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;
    }
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  4. #4
    SitePoint Addict Smola's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren View Post
    If your using a relatively modern version of PHP, you can try and do it without cURL and see what that shows up
    Got the same result. Running 5.2.x =[
    Humbly,

    Smola

  5. #5
    SitePoint Addict Smola's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Force flow, are all of the functions within your function native to php?
    Humbly,

    Smola

  6. #6
    SitePoint Enthusiast SimonJ621's Avatar
    Join Date
    Mar 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Addict Smola's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is embarassing.

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

    *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.
    Humbly,

    Smola

  8. #8
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Smola View Post
    Force flow, are all of the functions within your function native to php?
    Yep. 5.1.3 and forward.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain


Bookmarks

Posting Permissions

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