note 108431 deleted from function.file-get-contents by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:38:14 +0000 |
| Subject: | note 108431 deleted from function.file-get-contents by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215235@lists.php.net to get a copy of this message | ||
Note Submitter: dustin at dumontproject dot com-spam sux
----
For those who use file_get_contents for JSON or other RESTful services - like my architecture did
for a big site - this will probably help a lot.
We struggled with having the site using get urls that would go through our load balancer instead of
hitting the local server.
What we did was load this function through a local url and set the Host: header for our virtualhost
entries on the site we wanted to laod.
This code solved our issue:
<?php
//set the header context stream for virtualhost lookup
$context = stream_context_create(array('http' => array('header' =>
'Host: www.VIRTUALHOSTDOMAIN.com')));
//use a localhost url or alternatively 127.0.0.1 ip
$url = 'http://localhost/rest-service/?get-user&id=######';
//fetch the data through webserver using the Host http header we set
$data = json_decode(file_get_contents($url, 0, $context));
//verify you have your data
var_dump($data);
?>