/    Sign up×
Community /Pin to ProfileBookmark

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

[code=php]$json = file_get_contents(“http://somesite.com/products.json;orderBy=priceAsc;pageSize=20”,true);[/code]

example 2

[code=php]$GetURL = “http://somesite.com/products.json;orderBy=priceAsc;pageSize=20”
$json = file_get_contents($GetURL,true);[/code]

example 3

[code=php]$OrderBy = “priceAsc”;
$PageSize = “20”;
$json = file_get_contents(“http://somesite.com/products.json;orderBy=”.$OrderBy.”;pageSize=”.$PageSize,true);[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmFeb 19.2014 — 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?
Copy linkTweet thisAlerts:
@joedalauthorFeb 19.2014 — 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.
Copy linkTweet thisAlerts:
@ginerjmFeb 19.2014 — 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.
Copy linkTweet thisAlerts:
@NogDogFeb 19.2014 — 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 [i]exact[/i] code that failed?). I would recommend doing some detailed testing, e.g.:
[code=php]
<?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($GetURL, true);
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($GetURL, true);
echo "3:".PHP_EOL.$GetURL.PHP_EOL.$json."<pre>";
[/code]
×

Success!

Help @joedal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...