Suppose I have an api: api.example.com, this code actually gets the contents of api.example.com/Browser/API/ping.php (this is a simple ping script). Now I want to be able to do something like api.example.com/ping/site_to_ping (keep in mind, the folder "ping" doesn't exist, neither do I have a folder for every existing site). This would then execute the following: file_get_contents("api.example.com/Browser/ping?*site_to_ping*");
Is this possible?
Asked
Active
Viewed 28 times
0

Florian De Bosscher
- 13
- 4
-
yes it's possible url rewriting. or routes if you are using frameworks like codeigniter/laravel https://stackoverflow.com/questions/1039725/how-to-do-url-re-writing-in-php – Madhawa Priyashantha Oct 22 '17 at 12:14
1 Answers
0
Sending an HTTP POST request using file_get_contents
is not that hard, actually : as you guessed, you have to use the $site_to_ping
parameter.
There's an example given in the PHP manual, at this page : HTTP context options (quoting) :
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $site_to_ping_name
)
);
$site_to_ping = stream_context_create($opts);
$result = file_get_contents('api.example.com/Browser/ping', false, $site_to_ping);
Basically, you have to create a stream, with the right options (there is a full list on that page), and use it as the third parameter to file_get_contents
-- nothing more ;-)
As a sidenote : generally speaking, to send HTTP POST requests, we tend to use curl, which provides a lot of options an all -- but streams are one of the nice things of PHP that nobody knows about... too bad...

M0ns1f
- 2,705
- 3
- 15
- 25