php curl or file_get_contents too slow on hosted site - php

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 ..

Related

Curl can not download file with php

I am trying to download a file using PHP and CURL, here is my code:
set_time_limit(0);
// Set file to write to
$file = fopen($nameGenerated['path'], 'w+');
$ch = curl_init();
if ($ch == false) {
die('Failed to create curl handle');
}
// Set Curl options
curl_setopt($ch, CURLOPT_URL, str_replace(" ", "%20", $uri));
curl_setopt($ch, CURLOPT_FILE, $file);
// get curl response
curl_exec($ch);
curl_close($ch);
fclose($file);
The file is empty and curl_exec always returns false. When I tried to get the errors using curl_error($ch) there was no error.
Note: I am using Yii2.
Can someone help me understand what's the matter?
I managed to fix the problem by using the ipv4 addess instead of the usual 127.0.0.1. Now I know the problem, the CURL was excepting an ip address, not the localhost or 127.0.0.1. Thanks for all the people who tried to help!

Best way to get output from another PHP file as a variable

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.

include an external .php file using php without changing config.ini

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.

When downloading a file with PHP, nothing happens?

I'm having trouble downloading a remote file via PHP.
I've tried using cURL and streaming, neither of which produces an error.
Here's my current code for streaming.
$url = "http://commissiongeek.com/files/text.txt";
$path = "/files/cb.txt";
file_put_contents($path, file_get_contents($url));
I'll be downloading a zip file when I get this working, but in theory this should work just fine...
The folder's permissions are set to 777, and as said before, no errors are being thrown.
What could cause this?
Split this up into multiple sections, so you can verify that each stage is working:
$url = 'http://...';
$txt = file_get_contents($url);
var_dump($txt);
var_dump(file_put_contents('/files/cb.txt', $txt));
The first dump SHOULD show you whatever that text that url returns. The second dump should output a boolean true/false depending on if the file_put failed or not.
It seems you have an absolute path that you are trying to save in. I believe you want the path changed to "files/cb.txt" instead and do not have any access to /files/
If you have allow_url_fopen set to true:
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
Else use cURL:
$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

How to download a file from server using ftp and how to save that file in my local folder

this is my code but this is not working whats a problem in this code.u have any another code.give me idea to download a file from server using ftp.i am trying this code in my localhost also in my own server.
$curl = curl_init();
$file = fopen("ftpfile/file.csv", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp:http://www.address.com/file.csv"); #input
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "myusername:mypassword");
curl_exec($curl);
I'm guessing one of two things: the fopen call failed (and you're not checking if it succeeded by seeing if $file !== false), or the double-use of _returntransfer AND _file is not acting as you expect.
returntransfer tells curl to return the retrieved data in the exec call, instead of outputting it directly to the browser. I suspect that if you did $data = curl_exec($curl); file_put_contents('ftpfile/file.csv', $data) you'd end up with a properly populatd file. So... either remove the returntransfer option, or eliminate the whole _file business and output the file yourself using what the _exec call returns.

Categories