I already find answers how to copy images over HTTP, but when I try to copy images over HTTPS then I get this:
Warning: copy(): SSL operation failed with code 1. OpenSSL Error
messages: error:14077458:SSL
routines:SSL23_GET_SERVER_HELLO:reason(1112)
This is code I use:
copy('https://www.metalacmarket.com/product-img/org/JpUSP3KgvgeeikNheRDi4CRg.jpg', IMAGES_PATH.'JpUSP3KgvgeeikNheRDi4CRg.jpg');
Any idea how to get images over HTTPS?
You could use cURL.
Here's an example adapted from the basic curl example.
$source = 'https://www.metalacmarket.com/product-img/org/JpUSP3KgvgeeikNheRDi4CRg.jpg';
$target = 'image.jpg';
$ch = curl_init($source);
$fp = fopen($target, "wb");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Related
I don't know the Python language. Can someone help me understand whether I correctly converted this to PHP? I want to upload a file to http://telegra.ph with PHP, but I don't know how to upload from PHP.
This code is an example from a Python Telegraph API wrapper library:
import requests
with open('/Users/python273/Desktop/123345.jpeg', 'rb') as f:
print(
requests.post(
'http://telegra.ph/upload',
files={'file': ('file', f, 'image/jpeg')} # image/gif, image/jpeg, image/jpg, image/png, video/mp4
).json()
)
And this is my PHP code that doesn't work. Is there something wrong?
$url = 'http://example.com/image.jpg';
$img = 'http://telegra.ph/upload/';
$ch = curl_init($url);
$fp = fopen($img, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
When I run this code, I see the following error:
Warning: copy(telegra.ph/upload): failed to open stream: HTTP wrapper does not support writeable connections
Your code:
$img = 'http://telegra.ph/upload/';
...
$fp = fopen($img, 'wb');
PHP fopen() documentation is here http://php.net/manual/en/function.fopen.php , check what $mode parameter means. Why are you trying to open remote file for writing, if you only need to read it?
I am using HighCharts and saving image of chart using cURL. It is working fine at my localhost. But when I try the same code on server, the image is blank. And in server error_log I found this warning message:
PHP Warning: imagecreatefromjpeg(): '10361254147.jpeg' is not a valid JPEG file in public_html/project/assign_img.php on line 34
The code that I am using is as below:
$imgNm = 'https://export.highcharts.com/charts/chart.2ce468213abe432aa1c288339f90171e.jpeg';
$img = 'xyz.jpeg';
$ch = curl_init($imgNm);
$fp = fopen($img, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
imagecreatefromjpeg($img);
Not sure where I am wrong.
If you just want to save your file and show in your browser you don't need cURL:
$imgNm = 'https://export.highcharts.com/charts/chart.2ce468213abe432aa1c288339f90171e.jpeg';
$filename = 'xyz.jpeg';
$image_data = file_get_contents($imgNm);
file_put_contents($filename, __DIR__ . '/' . $imgNm);
After this the file is save in same directory as your script (named 'xyz.jpeg') and you can use it in whatever way you want
I added a line for SSL verification to be false and the code worked fine for me.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Hope this help someone.
trying to copy() .MP3 file from remote url but it always fails.
$link = str_replace(' ','%20','http://mp3hungama.com/music/download.php?song_id=80522');
if (!copy($link,'/home2/muser/tmp/newname.mp3')) {
echo 'copy failed !';
}
$link url redirects to http://mp3hungama.com/music/audio//Indian%20Movies/Indian%20Movies%20Hindi%20Mp3%20Songs/Singh%20Is%20Bling%20(2015)/songs/Cinema%20Dekhe%20Mamma%20#%20Mp3HunGama.Com.mp3
same code works for others random urls like www.example.com/download.php?id=2332. what's the specifically problem here or any other way to do this job ?
I've tested your code and I also couldn't download the file, then, I've used curl an it work as expected:
$local_file = "/home2/muser/tmp/newname.mp3";//This is the file where we save the information
$remote_file = "http://mp3hungama.com/music/download.php?song_id=80522"; //Here is the file we are downloading
$ch = curl_init();
$fp = fopen ($local_file, 'w+');
$ch = curl_init($remote_file);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
curl_close($ch);
fclose($fp);
NOTE:
Make sure /home2/muser/tmp/ has write permissions.
TIP:
In the future, if you need to encode/decode a url, use urlencode or urldecode instead of str_replace
This link
already redirects to second link. So it's working already.
I want to know which one is better for trans-loading large files on my server. I have 2 options using curl or normal php fopen or fwrite. Below are both implementations. Could you please suggest which one is better and with reasons if possible.
Curl implementation
$fp = fopen (dirname(__FILE__) . '/localfile.tmp', 'w+');//This is the file where we save the information
$ch = curl_init(str_replace(" ","%20",$url));//Here is the file we are downloading, replace spaces with %20
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);
Normal php implementation
while(!feof($url)) {
fwrite($filename, fread($file, 1024 * 8 ), 1024 * 8 );
}
I'm trying to download this image with PHP to edit it with GD. I found many solutions for image links, but this one is a download link.
Edit:
$curl = curl_init("http://minecraft.net/skin/Notch.png");
$bin = curl_exec($curl);
curl_close($curl);
$img = #imagecreatefromstring($bin);
This is my current code. It displays "301 Moved Permanently". Are there CURLOPTs I have to set?
$curl = curl_init("http://minecraft.net/skin/Notch.png");
// Moved? Fear not, we'll chase it!
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// Because you want the result as a string
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$bin = curl_exec($curl);
curl_close($curl);
$img = #imagecreatefromstring($bin);
Here is an option to directly save the image to a file (instead of using imagecreatefromstring):
<?php
$fileName = '/some/local/path/image.jpg';
$fileUrl = 'http://remote.server/download/link';
$ch = curl_init($fileUrl); // set the url to open and download
$fp = fopen($fileName, 'wb'); // open the local file pointer to save downloaded image
curl_setopt($ch, CURLOPT_FILE, $fp); // tell curl to save to the file pointer
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // tell curl to follow 30x redirects
curl_exec($ch); // fetch the image and save it with curl
curl_close($ch); // close curl
fclose($fp); // close the local file pointer
fopen - depends on your php settings if url fopen is allowed.
or curl
see the fine php manual.