Upload image to telegra.ph with PHP - php

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?

Related

Highchart save chart image using cURL

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.

Download remote PDF

I'm trying to load and save a remote PDF to my server for a project, but the link has no file extension. It's a kind of secured link.
https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=F86A56B3-D12C-4E70-AE71-A8A422B3EA4E&LINK_PAGE=ITINERARYENC&TRANS_ID=A25D191B-B098-4F45-9217-FB6D2B70F803
When I open the link in the browser I can download the PDF file, but saving it with my script has no success.
Is there a way to save the PDF on my server with a script?
I tried following code without success:
$url ="https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=F86A56B3-D12C-4E70-AE71-A8A422B3EA4E&LINK_PAGE=ITINERARYENC&TRANS_ID=A25D191B-B098-4F45-9217-FB6D2B70F803";
getFileContents($url);
function getFileContents($url)
{
// Workaround: Save temp file
$img = tempnam(sys_get_temp_dir(), 'pdf-');
$img .= '.' . pathinfo($url, PATHINFO_EXTENSION);
$fp = fopen($img, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
return $result ? $img : false;
}
I found the script here : Downloading a large file using curl
To download a file (binary or not) you can use file_get_contents().
file_get_contents return the file content into a string.
//Download PDF content
$pdfContent = file_get_contents("https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=XXXXXXX-DDDD-4444-AAAA-XXXXXXX&LINK_PAGE=ITINERARYENC&TRANS_ID=AAAAAAAA-3333-5555-6666-222222222");
$fileName = "myPDF.pdf";
$fp = fopen($fileName, 'w+');
//Write content into the file
fwrite($fp, $pdfContent);
fclose($fp);
You can if necessary use options in file_get_contents to specify GET or POST method, Basic Authentication and more.
Thanks for your reply, but your code doesn't saves the PDF im looking for.
It safes a PDF with HTML content :
WWW.ENABLELOGISTICS Enable Logistics v95.4248
Welcome to Enable
Logistics Please enter your username and password to sign in.
Forgot Password?©2016 Bright People Technologies Pty Ltd
I have no clue how to dowload the pdf to my server.
Clicking the link only let's me download local.

Sending imagepng() object to server using cURL

I'm trying to take an imagepng() object and upload it to a different server using cURL. Here is the code I've got. To be clear, I know the imagepng file works correctly and is being generated because I can save it locally on the server the code is running on. I'm just not sure how to send that info to a new server. All of the variables are set before this code ($fileName, $imageObject, etc.):
$file = imagepng($imageObject, 'newTest'.$counter.'.png');
if($file){
$ch = curl_init();
$fp = $file;
curl_setopt($ch, CURLOPT_URL, 'ftp://'.$ftp_user.':'.$ftp_pass.'#'.$ftp_server.'/'.$fileName.'.png');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
}
The errors I am getting for every file (this code is in a loop processing multiple files) is. Of course {MY_URL} is replaced with the actual URL of my file:
Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in {MY_URL} on line 43
Warning: filesize() [function.filesize]: stat failed for 1 in {MY_URL} on line 44
So it appears that the file is the wrong format when it's being cURLed. What do I need to set it to in order to send it correctly?
Thanks for your help!
imagepng outputs image into a file,but does not return a file handle (it only returns TRUE in case of success). You need to use something like fopen to get a valid file handle. Try replacing $fp=$file with this:
$fp = fopen('newTest'.$counter.'.png', "rb");
Also, replace filesize($file) with filesize($fp).
In general, $file is just a boolean, not a file handle. Use $fp for every function that expects a file handle. Also, don't forget to close every file at the end of the loop (e.g. add the following line after curl_close):
fclose($fp);

curl vs normal file download

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 );
}

Copy Image from Remote Server Over HTTPS

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);

Categories