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!)
Related
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 �܈BRLA0K+*� �}�-�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�_��MyG ���ҹ��͛�n7�HK��e��8�W�E�|״�~���& }�G��T���Jd�!i�^�]�4�n�Uc����#�R#��[\��NDDx�� �.4�����i{�:��чw�;��ժۘh`�o��{_y�nE�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);
I am trying to convert this curl request to php.
curl -X POST -F "file=#test_img.jpg" "http://127.0.0.1:5000/FileUploading/UploadImage/"test_img.jpg"
This request is working correctly with the following flask-RESTful code
class UploadImage(Resource):
def post(self, fname):
file = request.files['file']
if file:
# From flask uploading tutorial
filename = secure_filename(file.filename)
file.save(os.path.join("Images/", filename))
return jsonify({"Path": "Images/" + filename})
else:
# return error
return {'False'}
However, the following php curl request returns error 400 as it seems the request.files parameter is empty.
$file_name = "test_img.jpg";
$post_data = array(
"file" => "#" . $file_name,
"type" => 'image/jpg'
);
$ch = curl_init();
debug_to_console(http_build_query($post_data));
$host = "http://127.0.0.1:5000";
$url = $host . "/FileUploading/UploadImage/" . $file_name;
debug_to_console($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$file_param = 'file=' . $file_name;
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
$headers = array();
$headers[] = "Content-Type: multipart/form-data";
debug_to_console($headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
debug_to_console('Error:' . curl_error($ch));
}
curl_close($ch);
I don't know what i am doing wrong in this php request
first off, don't try to upload files using the # method, it was deprecated in PHP 5.5, disabled-by-default in PHP 5.6, and completely removed in PHP 7.0.0, in modern PHP, use CURLFile to upload files. also in line 10 you don't urlencode $file_name, that's a bug. and in line 17 you're trying to encode it to application/x-www-form-urlencoded-format (via http_build_query), but the command you're trying to convert is using multipart/form-data-format, when you give CURLOPT_POSTFIELDS a string (as returned by http_build_query), curl will send it as application/x-www-form-urlencoded by default but to make curl send it in multipart/form-data-format (as you want), set CURLOPT_POSTFIELDS to an array, not a string, and curl will send it in multipart/form-data.
I'm trying to run CURL command in PHP to upload image to an API
the code mentioned in the doc was:
curl -u 15:tokenkeyiskmzwa8awaa https://api.bukalapak.com/v2/images.json -F file=#product-image.png -X POST
Have tried using mac terminal running this curl command (with proper username and password) and successfully got the result as it is uploading, however was not successfully doing it on PHP.
My php code is :
<?php
$data = array('file'=> '#'.$imagePath );
// this is an absolute path, give something like D://folder/path/on/my/webserver/image.jpg
$user = '1234567';
$pass = '123456';
$ch = curl_init();
$curl_options[CURLOPT_URL] = 'https://api.bukalapak.com/v2/images.json';
$curl_options[CURLOPT_CAINFO] = storage_path('app/cacert.pem');
$curl_options[CURLOPT_HEADER] = "Content-Type: application/x-www-form-urlencoded";
$curl_options[CURLOPT_POST] = 1;
$curl_options[CURLOPT_USERPWD] = $user.':'.$pass;
$curl_options[CURLOPT_POSTFIELDS] = http_build_query($data);
curl_setopt_array($ch, $curl_options);
$content = curl_exec($->ch);`
Curl version : 7.47.1
PHP version : 5.6.21
any feedback is very appreciated.
You did not posted the complete script: $curl_options is undefined, storage_path is related to Laravel but there is no import, etc.
Something like the following script should work as you expect (I didn't tested it since I don't have an account on this service):
<?php
$imagePath = 'path/to/your/image.ext';
$user = 'youruser';
$pass = 'yourpass';
$file = curl_file_create($imagePath);
$body = ['file' => $file];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.bukalapak.com/v2/images.json');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$pass);
curl_setopt($ch, CURLOPT_CAINFO, 'path/to/cacert.pem');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$result = curl_exec($ch);
curl_close($ch);
I need to upload 3 files along with variables in post data. This is what my call looks like -
$data['type1'] = new CurlFile($file1);
$data['type2'] = new CurlFile($file2);
$data['type3'] = new CurlFile($file3);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data, "var1: $val1", "var2: $val2");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data', "headkey: $headkeyValue"));
I am not able to get $app->request()->post('var1'); from slim framework. It is empty.
I am able to get the headkey from the Header as $app->request()->headers('headkey');
I am able to get the data in $_FILES
Here the sample curl request
$curlFile = curl_file_create($uploaded_file_name_with_full_path);
$post = array('val1' => 'value','val2' => 'value','file_contents'=> $curlFile );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$your_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
Don't forget to put the appropriate header.
You can also find good source here
Curl File Upload
to send file via CURL. Make sure that you are passing your file on file_contents key in above code.
It's due to the fact that all files uploaded with HTTP POST method are in $_FILES global variable. That's why you cannot access files by this way
$app->request()->post('val1');
but you can by using $_FILES
$_FILES description
An associative array of items uploaded to the current script via the HTTP POST method. The structure of this array is outlined in the POST method uploads section.
This is what I did in alignment to ssingh's answer:
$data['type1'] = new CurlFile($file1);
$data['type2'] = new CurlFile($file2);
$data['type3'] = new CurlFile($file3);
//New Code Added
$data['var1'] = "$val1";
$data['var2'] = "$val2";
//removed the trailing string
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
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;
?>