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;
?>
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);
Hello and thanks in advance, now I'm trying to upload a image to a prestashop 1.7 via webservice and I'm unable to insert a image in a product. I don't know what fails, because I don't get any response of the webservice, even with the debug enabled (I get the xml responses of the rest of the files, but not the ones from curl).
The variable $idProduct is a value passed to the function and is defined.
My code is the following:
$url = PS_SHOP_PATH."api/images/products/".$idProduct;
$dir_path_to_save = 'img/import/';
$img_path = getFile($remoteImageURL, $dir_path_to_save);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '#'.$img_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
echo print_r($response);
curl_close($ch);
The getFile function downloads the image to the server where is installed the prestashop and returns the path (returns a string with the real path where the image is stored, already tested).
I tried to make a form to upload the image (just for testing), but it returns "code 66 - unable to save this image". I don't know if this helps.
Thanks
UPDATE
A fellow programmer told me to use curl_file_create()
So I changed the $img_path declaration this way:
$img = curl_file_create($dir_path_to_save.'/'.basename($img_path));
Now everything works as intended.
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");
?>
I am new here and not a pro in php but need a small help. Actually I was given a code by my service prodvider to upload on my hosting. But when i uploaded that code on my hosting account it didnt work well and after contact the support I was told to use php curl code in some part to make it run. can anyone please help me with the issue.
Original Code:
<?php
$MobileNumber=substr($_REQUEST['VerifiedNumber'],-10);
$APIKey="<Your Dial2verify API Key Here>";
$SenderID="<6 ALPHABETIC CHARACTERS ONLY>";
$Message="<Your SMS Text Here>";
echo `curl -XPOST "http://host/SMS/SEND/$APIKey/$SenderID/$MobileNumber" -d "Msg=$Message"`;
?>
I don't know how to make the above code into curl code to make it work.
Thanks :)
The reason it did not work was that you where trying to call the command line version of cURL (and also doing the call wrong).
The best way is to use the php cURL module to make this call. Verify first that it is installed by creating an info.php file with the contents
<?php phpinfo();
If cURL is present on that page you are good to go.
Obviously I have not had the ability to test this code but something like this should work
$msisdn = substr($_REQUEST['VerifiedNumber'],-10);
$apiKey = "<Your Dial2verify API Key Here>";
$senderId = "<6 ALPHABETIC CHARACTERS ONLY>";
$message = "<Your SMS Text Here>";
$url = "http://host/SMS/SEND/$apiKey/$senderId/$msisdn";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('Msg' => $message));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo 'Response from server:';
print_r($result);
By using `, you are using CURL via command line.
1st, check if the CURL extension for php is installed properly on you environment. Check if something about CURL is available when you print <?php phpinfo();?>
2nd, if CURL is installed, check on php.net/curl to know how to use CURL.
Here a simple example to call your webservice :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.host.com/SMS/SEND/$APIKey/$SenderID/$MobileNumber");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"Msg=This+is+my+text+message");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$reply = curl_exec ($ch);
curl_close ($ch);
echo $reply;
?>
I'm trying todo a simple curl with a hashtag in target url - but getting errors.
I know this script works, i've used it many times before.
<?php
error_reporting(E_ALL);
$curl = curl_init('http://tools.pingdom.com/fpt/#!/d3YvU8/http://www.nginx-hosting.co.uk');
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
if ($result == ("")) {
echo ("Nothing to curl");
}
else {
echo $result;
}
?>
Here is the above script in action: http://www.nginx-hosting.co.uk/curl_test.php
As you can see the ouput is nothing like it's supposed to be. I ran the same command through SSH :
curl http://tools.pingdom.com/fpt/#!/d3YvU8/http://www.nginx-hosting.co.uk
But get this error message -bash: !/d3YvU8/http: event not found
I assume this is because the target url has an exclamation mark in it or a hashtag.
Could someone point me in the right direction please. Thanks in advance
I just tried your script it works just fine, about -bash: !/d3YvU8/http: event not found error you should use quotes like: curl '
http://tools.pingdom.com/fpt/#!/d3YvU8/http://www.nginx-hosting.co.uk'