$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'>
Related
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;
I know I can save an image using the following method:
$input = 'http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com';
$output = 'google.com.jpg'; // << How to save the image with proper extension?
file_put_contents($output, file_get_contents($input));
But what if I don't know the format of the downloaded image? What if it's "png"? How can I figure out the type of target image before saving it?
The best thing to do is just download it and rename the file later. That way, you only have to make one request.
Another thing you can do however is make an HTTP HEAD request. This gets all of the response headers, including the Content-Type header, so you can decide if you want that data or not before you download the file. However, not all servers support HEAD requests.
In any case, cURL is the easiest way to do this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/something');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$headers = curl_exec($ch);
This has been asked before, but most, if not all answers, were either solutions that didn't work in all situations or were unnecessary (like using getimagesize(), which downloads the entire image).
How would I check if a given URL leads to an image without having to hardcode image extensions (like .png', .jpg, etc.)?
You can read only file header by CURL and then detect if the header is image header or not.
curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $file_url);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$header = curl_exec($curl);
curl_close($curl);
if (strstr($header, 'image/png')) {
//file is image
}
I want to download multiple images from the following website:
www.bbc.co.uk
I want to do it by using PHP cURL, can someone help lead me in the right direction?
It would be nice to download all the images in one shot, but if someone can help me download maybe download just 1 or a bunch that would be great!
Edit: it would be a good idea to show what I have tried:
<?php
$image_url = "www.bbc.co.uk";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $image_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
// Getting binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);
// output to browser
header("Content-type: image/jpeg");
print $image;
?>
For some reason it is not working. It is to be noted I am an absolute amatuer at PHP and programming in general.
The above code you pasted isn't doing what you think it is.
$image = curl_exec($ch);
The $image variable doesn't contain any image, it actually contains the entire HTML of that webpage as a string.
If you replace
// output to browser
header("Content-type: image/jpeg");
print $image;
with:
var_dump($image);
You will see the html.
Something like this:
Try to find the actual champion image source and parse it accordingly
You just need the whole /Air/assets/images/champions folder right?
Nothing easier when you use FireFox and a Download plugin like "Download Them All", open the FTP folder (Eambo's link) where the champions' pictures are located, right clic, select the plug-in.
It's gonna list all the files, select them all or select those you need and start the download.
Also, if you own that game you can take a look in this path:
\League of Legends\rads\projects\lol_air_client\releases\<newest-version>\deploy\assets\images\champions
Since you will probably use that in your university, CHECK THIS and I hope you will find a solution.
I have an image on my FTP, here : /home/www/myImage.jpg
I need, in PHP, to retrieve this image and create a BLOB.
I use CURL and i don't know how do to that.
I have try this :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "ftp://MY_IP/home/www/myImage.jpg");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "LOGIN:PASSWORD");
$myBlobImage= curl_exec($ch);
curl_close($ch);
But with this, i retrieve nothing... Any ideas ? Thanks !
If You are running the code on the same server as the file is stored at, then You simply could do:
file_get_contents('PATH/TO/MyFile.Ext');
which loads the file's content. Now You could easily store it to the database to a BLOB column. You could base64_encode the data first or instead of file_get_contents use a byte stream to read stream of bytes...