I am trying to create a PHP script which can request data, such as HTML content, from an external server, then do something with the received content. Here is a generalized example of what I am trying to accomplish:
//Get the HTML generated by http://api.somesite.com/
//Now tack on the Unix timestamp of when the data was received
$myFetchedData = $dataFromExternalServer . "\n Data received at: ". time();
echo $myFetchedData;
I'm thinking I should use curl in here somewhere, but I am not sure after that. Could someone post a generalized example of how I could do this?
If you only need GET and allow_url_fopen
is enabled on your server, you can simply use
$data = file_get_contents('http://api.somesite.com');
simple methods
<?php
echo readfile("http://example.com/"); //needs "Allow_url_include" enabled
//OR
echo include("http://example.com/"); //needs "Allow_url_include" enabled
//OR
echo file_get_contents("http://example.com/");
//OR
echo stream_get_contents(fopen('http://example.com/', "rb")); //you may use "r" instead of "rb" //needs "Allow_url_fopen" enabled
?>
The best Way (using cURL):
echo get_remote_data('http://example.com'); //SIMPLE REQUEST;
//OR
echo get_remote_data('http://example.com', "var2=something&var3=blabla" ); //POST REQUEST;
(CODE: at GitHub )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With