Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP和CURL下载文件_Php_Curl_Qualtrics - Fatal编程技术网

使用PHP和CURL下载文件

使用PHP和CURL下载文件,php,curl,qualtrics,Php,Curl,Qualtrics,我试图从一个API下载一个文件,我正在成功地与之交谈,但是,当我点击该文件时,它只输出了一堆疯狂的字符。我相信它是.zip流,我只需要得到应该在那里的.csv文件 从API文档中: curl -XGET -H 'X-API-TOKEN: <API TokenZ' -H 'Content-Type: application/json' https://co1.qualtrics.com/API/v3/surveys/SV_50EhstBgHEG2voV/export-responses/26

我试图从一个API下载一个文件,我正在成功地与之交谈,但是,当我点击该文件时,它只输出了一堆疯狂的字符。我相信它是.zip流,我只需要得到应该在那里的.csv文件

从API文档中:

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
这就是它给我的:

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�\`%`�]+����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
echo
ing 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);

PKr�JO  Test survey.csv�气象组织�����¸�W���W��"��我��M6�M�˂�H����T���3.�$+�}{-ĶD93� 通过设置
CURLOPT\u RETURNTRANSFER
选项,curl\u exec
将在结果中返回下载的内容。因此,变量
$result
实际上包含文件的内容(
PKr�…是一个ZIP头)

因此,您只需要将变量的内容保存在文件中,而不是
echo
将其输入浏览器

比如说


通过设置
CURLOPT\u RETURNTRANSFER
选项,curl\u exec
将在结果中返回下载的内容。因此,变量
$result
实际上包含文件的内容(
PKr�…是一个ZIP头)

因此,您只需要将变量的内容保存在文件中,而不是
echo
将其输入浏览器

比如说


如果您将
$file
的地址放入浏览器,是否会提示您下载.csv?在命令行中运行该文件时是否有效?@ternalhour否,我将该文件放入浏览器并添加X-API-TOKEN头,我在响应负载中返回一个超长字符串。我假设它的原始文件数据,但我不确定如何对其进行编码或删除无论我需要做什么……这可能是一个安全风险!如果你将
$file
的地址放入浏览器,是否会提示你下载.csv?当你在命令行中运行它时,它是否工作?@EternalHour否,我将文件放入浏览器并添加X-API-TOKEN头,我在响应负载中返回一个超长字符串。我假设它是raw文件数据,但我不确定如何对其进行编码或我需要做的任何事情……这可能会带来安全风险!天哪!我知道它应该这么简单。@AterLux,我还需要将RETURNTRANSFER设置为0。谢谢!天哪!我知道它应该这么简单。@AterLux,我还需要将RETURNTRANSFER设置为0。谢谢!
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�\`%`�]+����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
echo
ing 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);