2

I am trying to create a simple web service that will give a result depending on parameters passed.

I would like to use file_get_contents but am having difficulties getting it to work. I have researched many of the other questions relating to the file_get_contents issues but none have been exactly the situation I seem to having.

I have a webpage: example.com/xdirectory/index.php

I am attempting to get the value of the output of that page using: file_get_contents(urlencode('https://www.example.com/xdirectory/index.php'));*

That does not work due to some issue with the https. Since the requesting page and the target are both on the same server I try again with a relative path: file_get_contents(urlencode('../xdirectory/index.php'));

That does work and retrieves the html output of the page as expected.

Now if I try: file_get_contents(urlencode('../xdirectory/index.php?id=100'));

The html output is (should be): Hello World.

The result retrieved by the command is blank. I check the error log and have an error:

[Fri Dec 04 12:22:54 2015] [error] [client 10.50.0.12] PHP Warning: file_get_contents(../xdirectory/index.php?id=100): failed to open stream: No such file or directory in /var/www/html/inventory/index.php on line 40, referer: https://www.example.com/inventory/index.php

The php.ini has these set:

allow_url_fopen, On local and On master

allow_url_include, On local and On master

Since I can get the content properly using only the url and NOT when using it with parameters I'm guessing that there is an issue with parameters and file_get_contents. I cannot find any notice against using parameters in the documentation so am at a loss and asking for your help.

Additional Notes:

  • I have tried this using urlencode and not using urlencode. Also, I am not trying to retrieve a file but dynamically created html output depending on parameters passed (just as much of the html output at index.php is dynamically created).

** There are several folks giving me all kind of good suggestions and it has been suggested that I must use the full blown absolute path. I just completed an experiment using file_get_contents to get http://www.duckduckgo.com, that worked, and then with a urlencoded parameter (http://www.duckduckgo.com/?q=php+is+cool)... that worked too.
It was when I tried the secure side of things, https://www.duckduckgo.com that it failed, and, with the same error message in the log as I have been receiving with my other queries.

So, now I have a refined question and I may need to update the question title to reflect it.

Does anyone know how to get a parameterized relative url to work with file_get_contents? (i.e. 'file_get_contents(urlencode('../xdirectory/index.php?id=' . urlencode('100'))); )

Community
  • 1
  • 1
Chris O
  • 689
  • 8
  • 22
  • 1
    file_get_contents('http://localhost/ffff/xdirectory/index.php?id=100'): – AldoZumaran Dec 04 '15 at 18:45
  • your `../` is a relative path and you should be using an absolute path, if it's on the same domain you do not need to get a URL at all but can just call the file with `$_SERVER['DOCUMENT_ROOT'].'/folder/address` . Also you don't need `urlencode` because that function is to stop strings being processed as URLs but you *do* want this address to be processed as a URL. – Martin Dec 04 '15 at 19:06
  • @AldoZumaran, thanks but no change using that url. – Chris O Dec 04 '15 at 19:13
  • @Martin, thanks. I have added some additional notes to help clarify. Thanks again. – Chris O Dec 04 '15 at 19:18
  • @ChrisO I have updated my answer for you, see if that works? – Martin Dec 04 '15 at 19:25
  • I have refined the question and will need to open another to address the https issue. – Chris O Dec 04 '15 at 20:48

4 Answers4

2

Unless you provide a full-blown absolute protocol://host/path-type url to file_get_contents, it WILL assume you're dealing with a local filesystem path.

That means your urlencode() version is wrongly doing

file_get_contents('..%2Fxdirectory%2Findex.php');

and you are HIGHLY unlikely to have a hidden file named ..%2Fetc....

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thanks Marc. I've tried now with urlencode() and without and get the same error in the log for both attempts. – Chris O Dec 04 '15 at 19:03
  • also note that f_g_c is completely unaware of your site's directory/file structure. it only knows what it can see on the filesystem. that means you can't use any web-based paths (especially re-written ones). only actual/real filesystem paths will work. – Marc B Dec 04 '15 at 20:04
2

call url with domain, try this

file_get_contents('https://www.example.com/inventory/index.php?id=100');
Martin
  • 22,212
  • 11
  • 70
  • 132
AldoZumaran
  • 547
  • 5
  • 23
  • Aldo, thanks. I tried as your recommendation, also tried the two ?? and only the relative path and both resulted in the same error in the log. – Chris O Dec 04 '15 at 19:09
  • Ok... wondered about the ??, never had seen that used before and now know why. – Chris O Dec 04 '15 at 20:42
0

It has been two years since I used PHP so I am just speculating about what I might try in your situation.

Instead of trying fetching the parsed file contents with arguments as a query string, I might try to set the variables directly within the php script and then include it (that is if the framework you use allows this).

To achive this I would use pattern: ob_start -> set the variable, include the file that uses the variable -> ob_get_contents -> ob_end_clean

It is like opening your terminal and running the php file with arguments.

Anyway, I would not be surprised if there are better ways to achieve the same results. Happy hacking :o)

EDIT: I like to emphasize that I am just speculating. I don't know if there are any security issues with this approach. You could of course ask and see if anyone knows here on stackoverflow.

EDIT2: Hmm, scrap what I said last. I would check if you can use argv instead.

'argv' Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string. http://php.net/manual/en/reserved.variables.server.php

Then you just call your php script locally but without the query mark indicator "?". This way you can use the php interpreter without the server. This is likely to be the most general solution because you can also use argv for get requests if I am understanding the manual correctly.

user1235831
  • 111
  • 1
  • 8
0

From reading your comments and additional notes, I think you don't want file_get_contents but you want include.

see How to execute and get content of a .php file in a variable?

Several of these answers give you useful pointers on what it looks like you're trying to achieve.

file_get_contents will return the contents of a file rather than the output of a file, unless it's a URL, but as you seem to have other issues with passing the URI absolutely....

So; you can construct something like:

$_GET['id'] = 100;
//this will pass the variable into the index.php file to use as if it was 
// a GET value passed in the URI. 
$output = include $_SERVER['DOCUMENT_ROOT']."/file/address/index.php";
unset($_GET['id']);

//$output holds the HTML code as a string,

The above feels hacky trying to incorporate $_GET values into the index.php page, but if you can edit the index.php page you can use plain PHP passed values and also get the output returned with a specific return $output; statement at the end of the included file.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • thanks again. I tried the document root and urlcode as suggested, same error. Also, I used the document root without the parameter and the html output showed up correctly, so, same result... works without parameter and does not work with parameter. – Chris O Dec 04 '15 at 19:36
  • perhaps the issue is within your index.php file? @ChrisO – Martin Dec 04 '15 at 19:40
  • thanks, I think... you may have lost me here. My index.php which is the target is to be used as a web service. While I am trying to get it working for me now, it will be used by other webs in the agency. If I can't get it to work for myself, I have doubts anyone else will be able to use it. I can follow the link (the full url htttps://www.... and the result based on the parameter passed to it works properly. I think your suggestion about needed file_get_contents needs to be able to run from the full url and not just as a relative path but then there's the problem with https error. – Chris O Dec 04 '15 at 19:53
  • @ChrisO what exactly are the HTTPs errors? Perhaps ask another SO question about these and then get these sorted out. – Martin Dec 04 '15 at 19:56
  • I think you are right, I'll open another question about why file_get_contents is not working when using https in the url. Error using https: [error] [client 10.50.0.12] PHP Warning: file_get_contents(https://duckduckgo.com/?q=php+is+cool): failed to open stream – Chris O Dec 04 '15 at 20:46
  • I can't say much about https, here, sorry. Good luck with it. @ChrisO – Martin Dec 04 '15 at 20:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97016/discussion-between-chris-o-and-martin). – Chris O Dec 04 '15 at 20:51
  • Have you read http://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-work-with-https This question seems a little old but looks like your issue. Also note the **WARNING** on the PHP page http://php.net/manual/en/function.file-get-contents.php when using SSL with Microsoft IIS (if you use that?) @ChrisO – Martin Dec 04 '15 at 20:54