Downloading Files with PHP and CURL - php

I am trying to download a file from an API that I am successfully talking to, however, when I hit the file, it outputs just a bunch of crazy characters. I believe it is the .zip stream, and I just need to get the .csv file that should be in there.
From the API documentation:
curl -XGET -H 'X-API-TOKEN: <API TokenZ' -H 'Content-Type: application/json' https://co1.qualtrics.com/API/v3/surveys/SV_50EhstBgHEG2voV/export-responses/2671b6ec-66e0-4e7b-90bc-77174363763d/file -o responses.zip
Here is what I have:
$file = 'https://co1.qualtrics.com/API/v3/surveys/'.$qualtrics_id.'/export-responses/'.$file_json['result']['fileId'].'/file';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo $result;
curl_close ($ch);
And this is what it gives me:
PKr�JOTest survey.csv�Wmo�����¸�w���W��"��i��M6�m�˂�h����T���3�$+�}{-ĶD93�<3~tܸ+�Dt� ���qW����eQam����� wR+��T̊\����$���(�;��B�]��jeō�[)�{ϭ��o�a�'i���7\V���F��N��*�{�{�����7��:�\I�\�4�cɕU�� �u��"�MS|��It;�ng��<�͢�$�'�g���C$�d9��~���b-T왗�~������mwsu�l�9H�w<�:ڒ������>����J��i�3� �J�~���+�xr��J���W�m�ѰR�V��*X��lYk�޲��Tc9Wl)FRS��B�]��%7����R�zYU��tۭP�T�Y��_��-����v8��VP�K��R� *n�2�ƈu�&����-V� ��y%��� Kh�,���o��e���Y�w���K�.#x�c;]����6��!��уһJƗ�vl+���T��-���A�DTa:P����x�JK��h�y� �t �܈BRLA0K+*� �}�-�AX�\`%`�]+����򒠳d,�n�׵g�R��[Y��]� 3�R)��x�?�ۭFjH%��]Ǥ�7����߄p���B"���#p�[����0d�+L���!�������?��x�JoV�Iz"�J���h%��lM�z��ZU![�x�40��$�0hY�0�xST��U�^��G"XT �;�ʞk�����sr�#��� ��F��,4D�v(T6r"w#QI���T;d7㊪^��bW6��pT�B��Q\~`�V�R�/�� ,q�R���\�U�B0ԇ6 ��>ռ�oT�֪|ߖ� � gߍ�x��ĩ����k4$�D�Z�Y���o+�Ƿٞ��d ��K:��r*��O�PXIYB�T�������E5-��������p����S��n�ڳ��$Ptʱ_��቎8��9�S�6��J`��'Q?هJ+X�,� ��#�C�)N���%�����[J`�y���J�"b�?�,� |�JT��ߔ��V���9���c�+��!7��X��}��V�P�nC�9fjվP�|E�0�A��`�}������'��r��H1o��8���ҕ�{�B4� �0>罐i���<�תFp��~��O���L:jEP�N#݈\�#T�4����� ��D����!�_�I<�� ��X��r�SK��MyDG*�h�>�8JM��F���=+��8���8!��e�3�5��K)X|`��3v�� oR���z�}�6������5����gu��5��j��W�I���#b,z���sH�yӜ�c�1����D�uA�:U��b���e�x!�Me>��fH���me�Vn�#d%u�ǔD6C���ynT`a�_��My޳G ���ҹ��͛�n7�HK��e��8�W�E�|״�~���& }�G��T���Jd�!i�^�؂]�4�n�Uc����#�R#��[\��NDDx�� �.4�����i{�:��чw�;��ժۘh`�o��{_y�nE�7���M^Nu����zd*t��h�!}#�9p�����v����_�-�����m��e#��X᧾���� [�����9�m.d����-��l�\�NϮ�+��Yӻ�nO��U7��|~���Xw�����6��Es���dw�<-Z�Ͽ!��8�#��&Z��]�OK��\�����3��3ӳ3��3�3�ٙ$>?�������:?s�����8�fq6�N��$�J�=�C]���F���� �FLY�.F�b��w���Y����״��~���ۏ­����g6�/�%�E<^��ы�q���a2M��?3o����w_q���;y����_��J��h<�b��eI���$�&i2��G���&Ϭ�.������d���I�'���E���t�ݦ�b�_���8ɲy�%q�$x��l����,�������PK��{pnZPKr�JO��{pnZTest survey.csvPK=�
I am not sure what to do with this, I would like to at least be able to download the zip file with the .csv files in there, however, it would be more than ideal to simply get the "Test survey.csv" file, but I haven't been able to do either, I have tried many different things such as:
// header('Content-Type: application/zip');
// readfile($result);
// $z = new ZipArchive();
// $h = $z->getStream( $result );
// stream_get_contents($result);
All with no luck, any help is appreciated. Thank you.

with the CURLOPT_RETURNTRANSFER option is set curl_exec will return downloaded content in the result. So, your variable $result actually contains the content of the file (PKr�... is a ZIP-header)
So, all you need, just to save the content of the variable in a file, instead of echoing it into the browser.
For example.
...
$result = curl_exec($ch);
file_put_contents('downloaded.zip', $result); // save the string to a file
curl_close ($ch);

Related

How send and receive a file using curl using standard multi part form request?

Any way to do this? I search in all SOF threads and anyone answer is unutil, for no break rules of the community I can put my codes,
but sincerely, it's so easy.
If multipart-form-data is standard way to upload using post, why many peoples using /CurlFile, curl_file_create...
I search in Google using date tool (1 year last) and nothing.
Example:
if (function_exists('curl_file_create')) { // php 5.5+
$cFile = curl_file_create("unit.txt");
} else { //
$cFile = '#' . realpath($file_name_with_full_path);
}
$post = array('extra_info' => '123456','file_contents'=> $cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://127.0.0.1/receive.php");
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$result=curl_exec ($ch);
echo ">>>". $result;
curl_close ($ch);
I need know how to receive
How to send:
<?php
system('cmd /c & cd C:\xampp\apache\bin & httpd -k restart &curl --form "content=#C:\xampp\htdocs\THE_FILE.file" http://10.1.1.37/create.php');
?>
and how to receive:
<?php
move_uploaded_file($_FILES['content']['tmp_name'], "THE_FILE_2.file");
?>

Unable to upload file using curl

I need upload a file using php. I have the following code that I am using
<?php
$file = realpath('hello_world.jpg');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.newocr.com/v1/upload?key=*My key*');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '#'.$file));
$result = curl_exec($ch);
echo $result;
curl_close ($ch);
?>
On executing I get the error msg
{"status":"error","message":"File must be uploaded.
https://www.newocr.com/api/"}
But when I manually make a form and upload the image using multipart it works fine. Is something wrong with my code or the issue is with the API
Executing it from command line like this
curl -X POST -F "file=#hello_world.jpg" http://api.newocr.com/v1/upload?key=*my api key*
Works fine
curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
Now, it is giving:
404 Page Not Found
The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.
Visit the Home Page
I had the same problem.
Solution:
<?php
$result = exec('curl -H "Expect:" -F file=#'.realpath($file).' http://api.newocr.com/v1/upload?key=KEY');
$result = json_decode($result, true);
return $result;
?>

Collecting file with PHP CURL after validating request downloads an empty file

I am doing a system where one of my sites goes to the other to get documents.
On the first site I am using Curl to make a request to get the file wanted:
I am using the solution from Download file from URL using CURL :
function collect_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://example.com");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
return($result);
}
function write_to_file($text,$new_filename){
$fp = fopen($new_filename, 'w');
fwrite($fp, $text);
fclose($fp);
}
$curlUrl = 'http://site2.com/file-depository/14R4NP8JkoIHwIyjnexSUmyJibdpHs5ZpFs3NLFCxcs54kNhHj';
$new_file_name = "testfile-new.png";
$temp_file_contents = collect_file($curlUrl);
write_to_file($temp_file_contents,$new_file_name);
I am testing downloading an image. If i use a direct URL into $curlUrl , for instance http://site2.com/file-depository/image.png it works perfect.
What I am doing is that the URL http://site2.com/file-depository/14R4NP8JkoIHwIyjnexSUmyJibdpHs5ZpFs3NLFCxcs54kNhHj is then parsed and checked against a database to match the document requested, once there is a document matched I need to provide this document to the Curl response.
I have tried many ways to read the file but everytime i am getting a file on the other end but it is only 1kb in size (45 expected) and when trying to open it i get an error unkown file type etc.
On the second site, once the URL is validated here is what I have:
$file = readfile('some-image.png');
echo $file;
I am guessing there is part of the information which belongs to the file missing but can't figure it out, any pointers appreciated!
I have replaced
function write_to_file($text,$new_filename){
$fp = fopen($new_filename, 'w');
fwrite($fp, $text);
fclose($fp);
}
by file_put_contents($new_file_name,trim($temp_file_contents));
Please note the trim(), the issue was that I was apparently collecting some empty space in front of the file content.

How to post files using cURL

I am using cURL for the first time. I have to send one image file and one audio file posted by the user.
My cURL code is working, but instead of an image file and an audio file my code is sending a .tmp file.
I googled it, but in every example I found they have used realpath of file directly.
I tried to find real path of the file, but I didn't find any solution.
Here is my code block in which I am collecting all data in an array to pass it to cURL:
$name = $_POST['name'];
$image = $_POST['image']['name'];
$imagetmp = $_POST['image']['tmp_name'];
$imagesize = $_POST['image']['size'];
$imagepath = '#'.$imagetmp;
$audio = $_POST['audio']['name'];
$audiotmp = $_POST['audio']['tmp_name'];
$audiosize = $_POST['audio']['size'];
$audiopath = '#'.$audiotmp;
$data = array("name" => $name, "image"=> $imagepath, "audio" => $audiopath); //array to sned data using cURL
//my cURL code to post data
Where am I doing wrong? How to send files using cURL?
This is what I used to post file data:
$filedata = file_get_contents('file_location/filename.jpg');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/octet-stream'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$return = curl_exec($ch);
curl_close($ch);
You can use file=#path with curl
curl -kiSs -X POST https://<domain>/path/to/api/files/ \
-F "param1=param2" \
-F "file=#/path/to/test.xlsx" \
-H "Authorization":"bearer eyJhbGciOiJIUzI1NiIsIn"
File successfully uploaded (test.xlsx!)

Updating Twitter background via API

I'm having a little trouble updating backgrounds via Twitter's API.
$target_url = "http://www.google.com/logos/11th_birthday.gif";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $html), 'POST');
When I try to pull the raw data via cURL or file_get_contents, I get this...
Expectation Failed The expectation given in the Expect request-header
field could not be met by this server.
The client sent
Expect: 100-continue but we only allow the 100-continue expectation.
OK, you can't direct Twitter to a URL, it won't accept that. Looking around a bit I've found that the best way is to download the image to the local server and then pass that over to Twitter almost like a form upload.
Try the following code, and let me know what you get.
// The URL from an external (or internal) server we want to grab
$url = 'http://www.google.com/logos/11th_birthday.gif';
// We need to grab the file name of this, unless you want to create your own
$filename = basename($url);
// This is where we'll be saving our new file to. Replace LOCALPATH with the path you would like to save the file to, i.e. www/home/content/my_directory/
$newfilename = 'LOCALPATH' . $filename;
// Copy it over, PHP will handle the overheads.
copy($url, $newfilename);
// Now it's OAuth time... fingers crossed!
$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $newfilename), 'POST');
// Echo something so you know it went through
print "done";
Well, given the error message, it sounds like you should load the URL's contents yourself, and post the data directly. Have you tried that?

Categories