Hi i want to include an external .php file inside my php file.
this .php file has some variable declaration needed inside my page.
i tried using file_get_contents but it just echoes the lines inside.
tried:
function getter($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo getter('www.somehting.com/phpfile.php');
-still not working
also tried:
$code = file_get_contents($url);
eval($code);
is there anyway for me to do this without changing the config.ini?
I have access to .htaccess on the page where the file should be included.
the file is on a different server "rackspace".
Thank You.
You cannot include an external php file, unless the .php file is outputing the php source. Otherwise you will just get the interpreted php file from the external source.
The only thing you can do is output the .phps (source) so that you can access the actually code. Like I said above, you would be accessing the processed / interpreted php file, which would do you no good.
If you have access to the other server, you can try outputing the file.phps instead of file.php
If you load a file on an external server by web address, Apache will show the page after the php has been finsished.
So it will return
Hello world!
Instead of
<?php
echo 'Hello world!';
?>
You could, however, try to connect to the external server with ftp connection and retreive the file you want. This will require you to have administrator access to the server its hosting.
Related
So I found this page: Load external XML and save it using PHP to help me out, but it doesn't seem to work for me.
I'm trying to do the same thing by loading an external xml file and saving it (with no changes to the xml file) into my website directories as a batch system. I have already dynamically created all the directories needed.
ex:
/xml/en/281
Now what I'm trying to do is load the company's xml files (https://thiscompany.com/xml/en/281/18511095_en.xml) and save it in my own directory as the same name, 18511095_en.xml in the 281 directory.
I've been researching and I am getting lots of simplexml_load_file and DOMDocument examples but I'm not getting the results I needed.
For all sake and purposes here is the code: (I'm changing the url of the actual xml file because my client doesn't want it out there.
EDITED from the responses below
$url = "https://thiscompany.com/xml/en/281/18511095_en.xml";
$timeout = 10;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
$response = curl_exec($curl);
file_put_contents(__DIR__ . "/xml/18511095_en.xml", $response);
I'm assuming the problem is with the saveXML path. The xml directory is at the root of my website. Do I need to include, http://www.....com?
xml, en, and 281 all have 0777 permissions.
Any help would be appreciated.
You need to provide the path on your filesystem, not a URL.
e.g. "/var/www/www.example.com/htdocs/xml/en/281/18511095_en.xml"
Here's a simple way to copy a remote file into the same directory as your PHP file:
$url = "http://www.test.com/xmlfile.xml";
$contents = file_get_contents($url);
file_put_contents(dirname(__FILE__) . "/xmlfile.xml", $contents);
You can also accomplish this without having to use simplexml_load_string which will use more memory on your server. Instead try the following:
$xml = file_get_contents("https://thiscompany.com/xml/en/281/18511095_en.xml");
file_put_contents("/var/www/my/full/data/path/filename.xml", $xml);
$xml = simplexml_load_string($response);
$xml->saveXML("/xml/en/281/18511095_en.xml");
First of all, you should use a relative path instead of an absolute one. In this example you are trying to save the file under the /xml folder on the root filesystem and there's most likely no such directory and you don't have permissions to write to that directory (assuming it exists). Use a relative path.
Secondly, you don't need to parse the XML file, you can save it directly. Here's a working example:
$response = curl_exec($curl);
file_put_contents(__DIR__ . "/xml/en/281/xmlfile.xml", $response);
Further reading: Absloute path vs relative path in Linux/Unix
I'm using cUrl to get the file's contents of the same website's page, and writing to another file ( To convert dynamic php file into static php file for menu caching purpose )
$dynamic = 'http://mysite.in/menu.php';
$static = "../menu-static.php" ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$dynamic);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
file_put_contents($static, $file);
die();
It works perfect on localhost. But taking too much time when it is running on hosted website, and at last even the output file ($static = "../menu-static.php") is empty.
I can't determine where is the problem .. Please help
I've also tried file_get_contents instead of cUrl with no luck ..
I need PHP code to download file from one server to another. It's defined by variable $HasPrevod. That's the file which I need to download. Also there's simple form where I put link and the output is that variable $HasPrevod, so I had to put it in a href to get link easily to download.
Download. and download it manually and upload via Filezilla on my server. Is there solution which will download content from that variable and put it on my server in some folder. I tried with cURL but nothing happened because I'm newbie and I've made mistake for shure, and I don't know much about PHP and cURL.
So it should be something like this $HasPrevod file download -> my server / folder, and echo file name.
$prevod = $_POST['prevod'];
$url = file_get_contents("$prevod");
$PrevodLink = preg_match('!http://[a-z0-9\S\ \-\_\.\[\]\[\]\/]+\.(?:srt)!Ui', $url, $match1);
$HasPrevod = $match1['0'];
<form action="prevod.php" method="post">
<input name="prevod" type="text"/>
<input type="submit" value="Search"/>
</form>
Thats the code I'm using to put link and get path to the file which is located on another website not my own, then I have to download manually and upload on my server via Filezilla.
Download
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "prevodi/". $HasPrevod;
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
file_put_contents('path/to/file.ext', file_get_contents($HasPrevod));
If you have enough permission, try with exec('wget http://www.example.com/myFile.zip');
If not, try with file_put_contents() method. At last if that now works, try to var_dump() some states of code to debug it.
My plan is to create php 'template' files which will create various reports for the users of the site on demand. What I want to do is get the output of the php file and save it as a new file on the server. The curl and file creation works, but ...
Problems and questions :
When using curl I no longer have access to any of the $_SESSION variables which are set for the user logged in. I need to be able to access these in my output.php file to actually create the correct content I want in the file. Is there a way to pass/allow access to the $_SESSION variables? If not, is there a way I can post data along with curl... such as posting the user ID or something along those lines?
Is the mode required when using recursive = true for mkdir?
Lastly... my original plan was to store these 'template' files outside of the public_html directory so there was no direct access to the files... only through my scripts although it appears curl can only save the output of the php file when given a url. With that said, it appears the files must be accessible via the web. Is there a way to give curl a filepath such as '/home/test/template/output.php instead and still process the file and save the actual output of it?
I am up for any suggestions that work EXCEPT file_get_contents as I have fopen disabled. Would using output_buffer instead be better in this situation?
example call to create a report :
get_archive('http://www.test.com/template/output.php', '/home/test/public_html/reports/', 'newreport.html');
function get_archive($file, $local_path, $newfilename)
{
// if location does not exist create it
if(!file_exists($local_path))
{
mkdir($local_path, 0755, true);
}
$out = fopen($local_path.$newfilename,"wb");
if ($out == false){
exit;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
}
simple example template file (output.php) : This would obviously be much more extensive, but you should get the idea.
<?php
// These files can be included only if INCLUDE_CHECK is defined
require '/home/test/public_html/custom/functions/connect.php';
require '/home/test/public_html/custom/functions/functions.php';
require '/home/test/public_html/custom/functions/session.php';
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div class="row">
<div class="col-md-12">
<?php
//create report contents
echo $_SESSION['user']['account_id']; // returns nothing
?>
</div>
</div>
</body>
</html>
I need to include the output/result of a PHP file in another file.
I found info online about using curl to do this, but it doesn't seem to work so well, and so efficiently.
This is my current code:
function curl_load($url){
curl_setopt($ch=curl_init(), CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = "http://domain.com/file.php";
$output = curl_load($url);
echo "output={$output}";
Any recommendations on what I can use to make this more efficient/work better?
I'm looking for whichever method would be the fastest and most efficient, since I have a bunch of connections/users that will be using this file constantly to get updated information.
Thanks!
file_get_contents() may suitable for you
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
you can also use the file() or fopen()
$homepage = file('http://www.example.com/');
$homepage = fopen("http://www.example.com/", "r");
I ended up going with a PHP include statement, to include the other file.
I had forgotten to mention that the file was local, and this seems to make the most sense at this point - instead of echoing the result in the other PHP file, I'm just setting the result as a variable, and then pulling that variable in my other file.