including a remote file in PHP
To allow inclusion of remote files, the directive allow_url_include
must be set to On
in php.ini
But it is bad, in a security-oriented point of view ; and, so, it is generally disabled (I've never seen it enabled, actually)
It is not the same as allow_url_fopen
, which deals with opening (and not including) remote files -- and this one is generally enabled, because it makes fetching of data through HTTP much easier (easier than using curl)
Read a remote file in php
Option 1 - Curl
Use CURL and set the CURLOPT_FOLLOWLOCATION
-option to true:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http//example.com");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(curl_exec($ch) === FALSE) {
echo "Error: " . curl_error($ch);
} else {
echo curl_exec($ch);
}
curl_close($ch);
?>
Option 2 - file_get_contents
According to the PHP Documentation file_get_contents() will follow up to 20 redirects as default. Therefore you could use that function. On failure, file_get_contents() will return FALSE and otherwise it will return the entire file.
<?php
$string = file_get_contents("http://www.example.com");
if($string === FALSE) {
echo "Could not read the file.";
} else {
echo $string;
}
?>
Including remote HTML file
Try cURL:
http://php.net/manual/en/book.curl.php
Basic example:
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Ref: http://php.net/manual/en/curl.examples-basic.php
Return value from include on a remote file
Try using file_get_contents() instead of include.
This writes the file into a variable [causing the script to execute remotely], but won't run the response locally.
Alternatively If you have the ability to use cURL, it is safer and probably quicker.
A small snippet to remotely file_get_contents();
function curl_get_contents($url){
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($c);
if (curl_getinfo($c, CURLINFO_HTTP_CODE) > 400) $res = false;
curl_close($c);
return $res;
}
By way of explanation
- RETURNTRANSFER puts the response of the curl request into a variable (instead of printing to screen).
- CURLOPT_FOLLOWLOCATION has not been set, so if the page has been moved, curl will not follow it. You can set that, or have it set based on a second argument.
- HTTP_CODE, if above 400 (an err code, presumably 404 or 500) will return false instead of the fancy custom 404 page that might be setup.
In my testing, get_headers() is more reliable than curlinfo_http_code but requires a second call to the page being included, which can make things go awry.
eg.if (!strpos(200, get_headers($url)[0])) return false;
Including remote files in PHP
If you mean that the function 'makeConnection()
' is declared in the external file.php then you won't see the function because the external server is parsing the php code NOT sending you the raw code.
You'll need to save the external file.php
to something like file.txt
so that the server returns the contents without processing.
Try it and see if it works for you.
If it does then you can plan something more efficient.
Related Topics
How to Check If the Request Is an Ajax Request With PHP
Avoid Resending Forms on PHP Pages
Override PHP Base Dependency in Composer
Extract Jsonp Resultset in PHP
How to Get Single Value from This Multi-dimensional PHP Array
PHP Date() Format When Inserting into Datetime in MySQL
Access PHP Var from External JavaScript File
Disable Warnings When Loading Non-well-formed HTML by Domdocument (PHP)
PHPmailer Godaddy Server Smtp Connection Refused
PHP Equivalent of .Net/Java'S Tostring()
Sending Json to PHP Using Ajax
How to Properly Escape HTML Form Input Default Values in PHP
What Exactly Are Late Static Bindings in PHP
Escaping Single Quote in PHP When Inserting into MySQL
PHP Json_Encode Not Working on Arrays Partially