Save image from url with curl PHP
try this:
function grab_image($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
and ensure that in php.ini allow_url_fopen is enable
Save image from url with curl and file_put_contents PHP
In your $_POST
the content of the img src
is coming in with certain special characters like &
encoded as &
.
If you open this URL in the browser, you get the same error: https://scontent.ftbs4-1.fna.fbcdn.net/v/t1.0-9/39900479_1856467244440953_5986986678919626752_n.jpg?_nc_cat=0&oh=6262ebe636e7328f0471af2820fd4050&oe=5C03BEC7
.
You can reverse this escaping using html_entity_decode
. If I change this line the curl works:
$img_link = html_entity_decode(array_pop($regexResult));
how to get image from curl
Your script is correctly written. I have tried and works fine, but you need to set image headers in order to let navigator show the image.
Example:
header("Content-Type: image/jpeg");
$url = 'https://images.nga.gov/en/web_images/constable.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
$res = curl_exec($ch);
$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch) ;
echo $res;
But please note your image url is down, that's why you are getting 404 error.
Regards
Save image using cURL
As it turns out the problem was a simple one.
-The first clue was that the command in terminal was working but the same command with shell_exec()
was returning an error.
-The second clue was that as delboy1978uk mentioned the error was not 401 not authenticated but a 403 non authorized.
So there had to be a problem with the URL or parameter.
I printed out the URL but found no error....
So long story short, the problem was with the special characters in the URL. When I printed the URL the browser displayed the &
character correctly not as the function got it as a parameter &
.
So if I feed URL to htmlspecialchars_decode()
prior to run the command then it works flawlessly.
So lookout for special characters in the URL!
image download by php curl or file_get_content
First of all the link you provided is not an image. It outputs..
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>AllAccessDisabled</Code>
<Message>All access to this object has been disabled</Message>
<RequestId>35DFBD44D75B45C4</RequestId>
<HostId>
n1TT+gOUuMJ3RydGND/s3QL73JJEadz/2uQutK4NNUDbEaGX6EI0Y8a/6eDHLR6H
</HostId>
</Error>
Tested & Works !
Below code reads an image from an URL, saves it and then opens/renders on browser.
<?php
$image ="http://blog.stackoverflow.com/wp-content/uploads/stackoverflow-logo-300.png";
$ch = curl_init($image);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("test.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
header ("Content-Type: image/png");
readfile("test.jpg");
OUTPUT :
Or
If you want just to render it.. Do like this.
<?php
readfile("http://blog.stackoverflow.com/wp-content/uploads/stackoverflow-logo-300.png");
header ("Content-Type: image/png");
Downloading Images from list of URL to server using PHP
As the file
doc says, the file's newlines are preserved in the resulting array:
Each element of the array corresponds to a line in the file, with the newline still attached.
Pass the FILE_IGNORE_NEW_LINES
flag to the file(...)
call.
Otherwise, most or all of your $line
values will end with a '\n'
or '\r'
character.
You should probably also pass FILE_SKIP_EMPTY_LINES
.
$lines = file( 'List.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ); //the list of image URLs
Downloading an Image From an External URL With cURL
See here:
http://www.edmondscommerce.co.uk/php/php-save-images-using-curl/
function save_image($img,$fullpath){
$ch = curl_init ($img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
if(file_exists($fullpath)){
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp);
}
Other articles/sources:
http://forums.digitalpoint.com/showthread.php?t=371632
http://www.bitrepository.com/download-image.html
http://php.bigresource.com/Track/php-Jjg3DsKY/
Related Topics
Trying to Get Property of Non-Object - Laravel 5
PHP Foreach Pass by Reference: Last Element Duplicating (Bug)
PHP MySQLi Prevent SQL Injection
Why Won't My PHP App Send a 404 Error
Formatting Datetime Object, Respecting Locale::Getdefault()
Paypal Access - Ssl Certificate: Unable to Get Local Issuer Certificate
Why Is Using a MySQL Prepared Statement More Secure Than Using the Common Escape Functions
$_Post Vs. $_Server['Request_Method'] == 'Post'
Warning: MySQL_Fetch_Array() Expects Parameter 1 to Be Resource, Boolean Given In
PHP Variables in Anonymous Functions
Converting a Number With Comma as Decimal Point to Float
How to Require a Fork With Composer
Simple PHP Strpos Function Not Working, Why
Truncate Text Containing Html, Ignoring Tags
Unformat Money When Parsing in PHP
Creating a Thumbnail from an Uploaded Image