Can not extract zip created by PHP and uploaded via cURL - php

I am trying to zip up a website and then upload to a CDN. But I am getting an error message when the zip is opened by the cdn: Error extracting zip: Zip end of central directory signature not found
The website is not complex, just some root level html files and an image folder. I created the zip file with PHP and when I open a downloaded copy on my computer it opens perfectly. And when I manually upload it to the cdn it extracts perfectly.
But when I upload it via PHP/cURL I get the error. So assume I am doing something wrong with my cURL functions. My apologies if this is a simplistic mistake, it is my first foray into cURL.
My upload code:
<?php
$file = "/path/to/mytest.zip";
$authorization = "Authorization: Bearer my_access_token";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.domain.com/bla/bla/bla");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, [$authorization, 'Content-Type: application/zip']);
$cfile = new CurlFile(#$file, 'application/zip');
$data = array('data-binary' => realpath($cfile));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($curl);
curl_close($curl);
?>
Any suggestions appreciated.

You should check this snippets on the RFC: https://wiki.php.net/rfc/curl-file-upload
The correct implementation of CurlFile is:
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = new CurlFile('filename.png', 'image/png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);

Related

How to get file extension with CURL

So, I've got to download a file to do a verification, I must check if the downloaded file is an XML or an ZIP.
I have the following code:
$url = $return->DocXMLLink; //link to download the file
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$response = curl_exec($ch);
//by here I may get the file extension and do something
Thanks in advance

How can i upload and post an image to linkedin using api?

I'm trying to create an app for posting images to linkedin. Linkedin has an api which accepts an image url and a message, but it seems to be not the exact way to post an image since it is showing my image as a link instead of an image. But in the case of Buffer App, it displays full image, not the URL. Is there any way to post images to linkedin via api. Anyone please help me to sort this out.
Use Linkedin V2 API.Below code will upload the image.
curl -i --upload-file /Users/peter/Desktop/superneatimage.png --header "Authorization: Bearer redacted" 'https://api.linkedin.com/mediaUpload/C5522AQGTYER3k3ByHQ/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJbrN86Zm265gAAAWemyz2pxPSgONtBiZdchrgG872QltnfYjnMdb2j3A&app=1953784&sync=0&v=beta&ut=2H-IhpbfXrRow1'
I suggest you to use Guzzle HTTP client to exicute this in your PHP application
This works for me
$upUrl = "Your Url";
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $upUrl);
curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Content-Type: application/binary'));
$path = "./uploads/image.jpg";
$file = fopen($path, 'r');
$size = filesize($path);
$fildata = fread($file, $size);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $fildata);
curl_setopt($cURL, CURLOPT_INFILE, $file);
curl_setopt($cURL, CURLOPT_INFILESIZE, $size);
$response = curl_exec($cURL);
curl_close($cURL);

Slack. How to download image in url_private property using PHP?

I am trying to download a png image from Slack and save it into a file but the only thing that gets saved into the file is HTML code for the login page of Slack.
This is what I have tried.
$url = 'https://files.slack.com/files-pri/XXXXXX-XXXXXXX/download/2016-07-11.png';
$ch = curl_init($url);
$fp = fopen('/assets/tmp/1.png', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer xoxp-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXXXXXXX'
));
curl_exec($ch);
curl_close($ch);
fclose($fp);
If I access the link in browser while logged into Slack, the image downloads just fine, but not through the PHP code. Any help would be appreciated.
I needed an extra permission scope to read files. I added files:read permission scope to my application from Slack Developer Console and the code works as expected.

Get rid of Content-Disposition prefix added to files while uploading

I write a script to upload files by php curl to oipenstack server. I got the token and the address to the upload directory.
When I use curl in CLI it works. But when I use my php script some code is added to the begining of the file so the file is changed. When the file is a video file it gets damaged and media players cannot play it anymore.
I have noticed the changes that are made by php curl. I compared the file before upload and after upload. The difference is some code added before the file content that I figured out in hex editor.
Let's watch them in screenshots (before / after):
So something about Content Disposition, name, filename are added there. I would like to fix my php curl script not to add any prefixes and change files at all. My php code is this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://abc-de.fghijklm.com/v1/AUTH_8a619275-f933-4da1-b289-0c06a1a2a3a4/bcd/drop93.avi");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$headers = array();
$headers[] = "X-Auth-Token: AUTH_tkaacb23cace324039a3be9c81b1b2b3b4";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$curlFile = curl_file_create ('./drop.avi');
$post = array (
'file_contents' => $curlFile
);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

Upload a URL as a file using PHP curl

I have images that are hosted on a CDN, and I want to upload that image to some other system via their API. Normally I would just use curl and put an # character in front of the file to upload. Since this is a URL, that won't work since it expects the file to be local. I don't want to copy it local first since the files could be videos and that could be time consuming.
It seems there should be a way to do this with PHP curl. I'm open to using sockets instead, but I'm not sure how to simulate how curl does the submission.
<?php
$save_file_url = file_get_contents($your_files_CDN_file_URL);
$fp = fopen('test','w');
fwrite($fp,$save_file_url);
$save_file = realpath('test') ;
$url = "http://URL_TO_SEND_FILE/get_file.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
"file_is_this"=> "#".$save_file,
"app"=>'test'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
?>
And you will recieve file at get_file.php.

Categories