www.webdeveloper.com
Results 1 to 5 of 5

Thread: file_get_contents doesn't work the way i want it to!

  1. #1
    Join Date
    Nov 2013
    Posts
    4

    file_get_contents doesn't work the way i want it to!

    Hi all

    I want to use variables in file_get_contents but it doesn't work.

    Only example 1 works, on my host or on my own machine running lamp server. I need either example 2 or 3 to work.

    I have searched the internet, but it doesn't seem to be a problem for anyone else.

    I don't get any error messages when I use examples 2 and 3, error messages are turned on, but i dodn't get any data either.

    The data is there, pasting the url in my browser shows it.

    Many thanks for reading this.

    example 1
    PHP Code:
    $json file_get_contents("http://somesite.com/products.json;orderBy=priceAsc;pageSize=20",true); 
    example 2
    PHP Code:
    $GetURL "http://somesite.com/products.json;orderBy=priceAsc;pageSize=20"
    $json file_get_contents($GetURL,true); 
    example 3
    PHP Code:
    $OrderBy "priceAsc";
    $PageSize "20";
    $json file_get_contents("http://somesite.com/products.json;orderBy=".$OrderBy.";pageSize=".$PageSize,true); 
    Last edited by NogDog; 02-19-2014 at 10:27 AM. Reason: changed quote tags to php tags

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    724
    Are you sure the url is correct? It looks like a get string, but it is not formatted like one. What happens if you browse to this url?

  3. #3
    Join Date
    Nov 2013
    Posts
    4
    Yes the url is correct and when I browse to it I can see the data.

    Example 1 works perfectly, but OrderBy and PageSize are variable, so I can't use example 1.

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    724
    if an echo of your geturl string in ex. 2 matches exactly what you use in ex. 1, I don't know.

    I do have a problem with ex 3 since the pagesize parm is hanging off the end of the url outside of quotes, but I'm probably mistaken.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    I cannot see any reason those different options should behave differently -- except for the missing semi-colon on the first line of no. 2 (which can't help but make me wonder if we are seeing the exact code that failed?). I would recommend doing some detailed testing, e.g.:
    PHP Code:
    <?php
    echo "<pre>";
    $json file_get_contents("http://somesite.com/products.json;orderBy=priceAsc;pageSize=20"true); 
    echo 
    "1:".PHP_EOL;$jsaon.PHP_EOL.PHP_EOL;
    $GetURL "http://somesite.com/products.json;orderBy=priceAsc;pageSize=20";
    $json file_get_contents($GetURLtrue); 
    echo 
    "2:".PHP_EOL.$GetURL.PHP_EOL.$json.PHP_EOL.PHP_EOL;
    $OrderBy "priceAsc";
    $PageSize "20";
    $GetURL "http://somesite.com/products.json;orderBy=$priceAsc;pageSize=$PageSize";
    $json file_get_contents($GetURLtrue);
    echo 
    "3:".PHP_EOL.$GetURL.PHP_EOL.$json."<pre>";
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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