I'm trying to use php to download files that are created and downloaded when you call to a url.
In this case i'm trying to programmatically download a txt file from the dutch weather institute wich can be done by going to(in this example) this url http://projects.knmi.nl/klimatologie/daggegevens/getdata_dag.cgi?lang=nl&byear=2016&bmonth=6&bday=20&eyear=2016&emonth=6&eday=22&variabele=FHX&variabele=FXX&variabele=TG&variabele=TN&variabele=TX&stations=249&submit=Download+data+set
Going to that url downloads the file.
I want to do this with php so I can use the contents of the file to make custom charts.
Does anyone know how to do this with php?
Using file_get_contents(): http://php.net/manual/en/function.file-get-contents.php
or using CURL functions: http://php.net/manual/en/function.curl-exec.php
EDIT: CURL Solution:
$url = 'http://projects.knmi.nl/klimatologie/daggegevens/getdata_dag.cgi?lang=nl&byear=2016&bmonth=6&bday=20&eyear=2016&emonth=6&eday=22&variabele=FHX&variabele=FXX&variabele=TG&variabele=TN&variabele=TX&stations=249&submit=Download+data+set';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$str = curl_exec($curl);
Try:
$content = file_get_contents("The URL here");
echo $content;
Related
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.example.com/xxx.png');
curl_setopt($curl, CURLOPT_REFERER, 'http://www.example.com');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
header('Content-type:image/PNG');
echo $result;
curl_close($curl);
The function header() didn't work, it always dispalyed binary data.
Maybe because I used these codes in the middle of webpage what has existed.
The webpage outputed some texts before header(), so it didn't work.
I want to get image by url, and display image directly, no need save file to disc.
So how can I do ? Please help me !!!
—————————
I need set referer, so I used curl.
Use the image directly as follows:
<img src ='http://www.example.com/xxx.png'>
i want to download image from this url
http://feelgrafix.com/959413-rococo.html
and this the source image
http://feelgrafix.com/data_images/out/28/959413-rococo.jpg
but when i download image from this source
file Download page url not download source image
this the code i used it
$url = 'http://feelgrafix.com/data_images/out/28/959413-rococo.jpg';
$ch = curl_init($imgURL);
$fp = fopen('image.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
and this Other way
$content = file_get_contents($url); file_put_contents('sadsdasd.jpg',$content);
i think that this the protect from server ..
no one can download OR see the image direct before open the page home
so what can i do ?
Instead of using curl function i recommend you to user file_get_content($url) to fetch the file and file_put_contents($path) to save it in your desired path and in case if any error it throws just usin # before both the function.
I am fetching data from API of a service provider (Say- http://serviceprovider.com).
From several parameter one is MP3 download Link (example- http://serviceprovider.com/storage/read?uid=475b68f2-a31b-40f8-8dfc-5af791a4d5fa_1_r.mp3&ip=255.255.255.255&dir=recording)
When I put this download link on my browser it saves it to my local PC.
Now My Problem -
I want to save this MP3 file in one of folder on my hosting space, from where I can further use it for playing using JPlayer Audio.
I have tried file_get_contents(), but nothing happened.
Thanks in advance.
Edit:
After reading Ali Answer I tried the following code, But still not working fully.
// Open a file, to which contents should be written to.
$fp = fopen("downloadk.mp3", "w");
$url = 'http://serviceprovider.com/storage/read?uid=475b68f2-a31b-40f8-8dfc-5af791a4d5fa_1_r.mp3&ip=255.255.255.255&dir=recording';
$handle = curl_init($url);
// Tell cURL to write contents to the file.
curl_setopt($handle, CURLOPT_FILE, $fp);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HEADER, false);
// Do the request.
$data = curl_exec($handle);
// Clean up.
curl_close($handle);
fwrite($fp, $data);
fclose($fp);
This created the file download.mp3 file on my server but with 0 bytes, i.e. empty.
The url used here is a download link example not a mp3 file that can be played with modern browser directly.
Function file_get_contents is used for reading local files. What you have is an URL and in order to fetch the contents, you need to do a HTTP request in your script. PHP comes with the curl extension, which provides you with a stable library of functions for doing HTTP requests:
http://php.net/manual/en/book.curl.php
Using curl to download your file could be done like this:
// Open a file, to which contents should be written to.
$downloadFile = fopen("download.mp3", "w");
$url = "http://serviceprovider.com/storage/read?uid=475b68f2-a31b-40f8-8dfc-5af791a4d5fa_1_r.mp3&ip=255.255.255.255&dir=recording";
$handle = curl_init($url);
// Tell cURL to write contents to the file.
curl_setopt($handle, CURLOPT_FILE, $downloadFile);
// Follow redirects.
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
// Do the request.
curl_exec($handle);
// Clean up.
curl_close($handle);
fclose($downloadFile);
You should probably add some error checking.
Task
Downloading binary files from a remote media processing server to a web server.
This works but I cannot capture the curl stdout output
$result = shell_exec('curl -v http://domain.com/images/dir/dir/dir/file.jpg --user username:password -o /usr/www/htdocs/images/dir/dir/dir/file.jpg');
Note:
I have had no luck using the PHP curl wrapper methods for this task, partly because I've never used PHP curl wrappers for FTP downloads.
Question
Can someone explain how to capture the output from the command that I am shelling out to or a simple example using PHP curl wrappers?
Here is what I tried and the part I'm stumped on is how to get the new file placed on the target server - The line that's wrong is the CURLOPT_FILE line. I don't want to have to create a stub file, open it and then write to it.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $ftpserver.$file['directory'].$filename); #input
curl_setopt($curl, CURLOPT_FILE, $dest); #output
curl_setopt($curl, CURLOPT_USERPWD, "$user:$password");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
Thanks
According to a comment in the PHP manual, you must be sure to close your curl stream AND your file handler before the file is written properly. I'll copy the example here for search purposes:
<?php
$fh = fopen('/tmp/foo', 'w');
$ch = curl_init('http://example.com/foo');
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
curl_close($ch);
# at this point your file is not complete and corrupted
fclose($fh);
# now you can use your file;
read_file('/tmp/foo');
?>
Also, I would debate the merits of using CURLOPT_RETURNTRANSFER with CURLOPT_FILE, as CURLOPT_RETURNTRANSFER tells curl to return the fetched results as the result of curl_exec(). You probably don't need that if you're just writing it to a file.
$dest needs to be a file resource opened via
$dest = fopen("filename.ext", "w");
does that work for you?
This fixed it by redirecting stdout output: 2>&1
So this works:
$result = shell_exec('curl -v http://domain.com/images/dir/dir/dir/file.jpg --user username:password -o /usr/www/htdocs/images/dir/dir/dir/file.jpg 2>&1');
It would be great to be able to use the PHP curl wrappers but not if it means opening a stub file on the source machine and then writing to it. The curl command line version just moves over the file, nice and neat. If anyone knows how to do what I'm trying to do using PHP wrappers, I'd love to see how you do it.
/**
*Downloads a binary file into a string
*#param string $file URL's file
*#param string $ref Referer
*#return string $downloaded_binary string containing the binary file
*/
function download_file($file, $ref)
{
$curl_obj = curl_init();
curl_setopt($curl_obj, CURLOPT_URL, $file);
curl_setopt($curl_obj, CURLOPT_REFERER, $ref);
curl_setopt($curl_obj, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl_obj, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl_obj, CURLOPT_MAXREDIRS, SPDR_MAX_REDIR);
curl_setopt($curl_obj, CURLOPT_FOLLOWLOCATION, TRUE); //followlocation cannot be used when safe_mode/open_basedir are on
curl_setopt($curl_obj, CURLOPT_SSL_VERIFYPEER, FALSE);
$downloaded_binary = curl_exec($curl_obj);
curl_close($curl_obj);
return $downloaded_binary;
}
I think the way to do this is with ob_start().
ob_start();
curl_exec($curl);
$result = ob_get_clean();
I am try to download image files from url from the following code, but it doesn't return the right content from the server. The image can be rendered in browser by loading the url or downloaded using curl in shell mode, but not in php execution. In php execution, the header's content type returned from the server is 'text/html' instead of 'image/jpeg' which it supposes to be.
Anyone has any ideas about this?
$url = 'http://count.koubei.com/showphone/showphone.php?f=jpg&w=104&h=10&bc=255,255,255&fc=0,0,0&fs=10&fn=arial&phone=LTIxMDM3MjIyOTc%3D%23dWBzmKEZpTh7YcWvOw%3D%3D';
$file_handler = fopen('phone.jpeg', 'w');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FILE, $file_handler);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_exec($curl);
curl_close($curl);
fclose($file_handler);
why not just use file_get_contents
like this
$img = file_get_contents("http://count.koubei.com/showphone/showphone.php?f=jpg&w=104&h=10&bc=255,255,255&fc=0,0,0&fs=10&fn=arial&phone=LTIxMDM3MjIyOTc%3D%23dWBzmKEZpTh7YcWvOw%3D%3D");
file_put_contents("photo.jpg",$img);