我需要帮助才能使它工作。我的请求在邮递员中运行良好,我可以通过单击按钮将响应保存为:在邮递员中访问我的文件(在此示例中为 zip 文件)。在 php 中,我试图访问同一个文件,但找不到如何访问。标头正在接收文件名、文件大小等。我试图将 getBody() 写入文件,但它不是我的文件,它只是一个字符串。
这是我执行发布请求的功能:
<?php
/**********************************************************************************************/
// Obtention des documents d'un workflow
// reçoit les params:
// MyworkflowId= l'id du workflow
// MyprefixUrl= url de départ de consigno
//
//
// utilise la variable session X-Auth-Token utilisable pour toutes les autres requêtes.
/**********************************************************************************************/
function getDocumentsFromWorkflow($MyprefixUrl,$MyworkflowId)
{
$request = new HTTP_Request2();
$request->setUrl($MyprefixUrl.'/workflows/'.$MyworkflowId.'/documents');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
'follow_redirects' => TRUE,
'store_body' => TRUE
));
$request->setHeader(array(
'Content-Type' => 'application/x-www-form-urlencoded'
));
//on passe notre variable session token à l'api pour certifier
//qu'on est authentifié.
$request->addPostParameter(array(
'X-Auth-Token' => $_SESSION['consignoToken']
));
try
{
$response = $request->send();
if ($response->getStatus() == 200)
{
echo "<br> Téléchargement des fichiers du dossier : ". $MyworkflowId;
echo "<br>". $response->getHeader('content-type')."<br>";
echo $response->getHeader('content-disposition')."<br>";
//$myfilename will be treated with substring of Content-Disposition
//for the sake of testing, i wrote a static name identical to the one received in this header
$myfilename = "2020-126.zip";
$filebytes = $response->getHeader('content-length');
//affichage de la taille du fichier en MB avec seulement 2 décimales.
$mysize = number_format($filebytes/1048576,2);
echo $mysize." mb<br>";
echo "<pre>";
$mycontent = $response->getBody();
//var_dump($mycontent);
//fonctionne ouvre le fichier sur le serveur et enregistre.
//
//file_put_contents("uploads/".$myfilename, $mycontent);
/*foreach ($response->getHeader() as $k => $v)
{
echo "\t{$k}: {$v}<br>";
}*/
// tested this to see if file download was triggered but instead of getting my file,
//it saves current php page as filename
//header('Content-Type: '.$response->getHeader('content-type'));
//header('Content-disposition: '.$response->getHeader('content-disposition'));
var_dump($response);
echo "</pre>";
}
else
{
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
}
}
catch(HTTP_Request2_Exception $e)
{
echo 'Error: ' . $e->getMessage();
}
}
?>
响应的 var_dump 给出:
object(HTTP_Request2_Response)#6 (9) {
["version":protected]=>
string(3) "1.1"
["code":protected]=>
int(200)
["reasonPhrase":protected]=>
string(2) "OK"
["effectiveUrl":protected]=>
string(97) "https://groupetcj.cloud-sandbox1.consigno.com/api/v1/workflows/5f4c3dd954e4f7113c44cfd6/documents"
["headers":protected]=>
array(12) {
["date"]=>
string(29) "Wed, 09 Sep 2020 13:21:49 GMT"
**["content-type"]=>
string(15) "application/zip"**
**["content-length"]=>
string(6) "702727"**
["connection"]=>
string(10) "keep-alive"
["x-auth-token"]=>
string(1900) "H4sIAAAAAAAAAIVWXXeiShD8R/cAxj2HxyUyxLkBnWGm+XgT0IAMxLvGD/z1t4c1EY3uviXDTHVVdXUny46WmZdXs4pO5WlqBhW1/1l2dJOPfH04y0a8zBtl5p2jpKXarJpup40qi+fpD1/I8UwkZrDOT7MJe/LDQ5XG5WG6fj8Goh4H6+Q0E/5TMHk7vj7TsojYY8wWTmlMaYq40zU0Gjs4vR0CwVXgJSNf0Co5paUf+SO883l/lkSmwnrVTNJguU43fOTw3IMqJCBYQ9plrTopDHtaIbdGdfmIn99sdkV03CKvbRIHhmjIRxpibRWUWcOrbPT2Bm7wr36XjPjT4H5ZxPxdeGq3MPl+YcFu8M1AXWWGGtKGbHNLolekTl+oykfBc2bZv6RFrBScLo0S7cUBNZwya6zf4N10k7eBwSx7lzWwLjy1R28uWC1XWUN2hatqrTkd0X0RO6rwgv80zyyCj6SBLrfKTU6cbRKNP/0s0zWz/IgdsFdPgUXWM8GsRPw8Bic2Svqeom4FB8QsU09WsyYts5dAoaZ39IdmbbopPGIsIlvrbZLoeEqx3wuPnMSItlhrs2y+vht5C/rtJsf6WQvb7PlQofdlobErGjF5pLzvI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bk496zfAQ/3B8PdQ+7M38Wbc9YEgI+5HuSFi6GnoTz6oRy/xIbpiJq88Ic7IZ1LVTiy/r6r8T1mB/cKwVCTq9129m2oiTDMe/pg197O8d96cF3rU3Nt93uOGaTfM1Nl2v/sLfy/ala8LhvD/pW3lcu601syD/eF++u1quR4yXbt8646qp//A8Bq1ldVDAAA"
**["content-disposition"]=>
string(35) "attachment; filename="2020-126.zip""**
["x-content-type-options"]=>
string(7) "nosniff"
["x-xss-protection"]=>
string(13) "1; mode=block"
["cache-control"]=>
string(46) "no-cache, no-store, max-age=0, must-revalidate"
["pragma"]=>
string(8) "no-cache"
["expires"]=>
string(1) "0"
["x-frame-options"]=>
string(4) "DENY"
}
["cookies":protected]=>
array(0) {
}
["lastHeader":protected]=>
string(15) "x-frame-options"
["body":protected]=>
string(5792) "PK�J)Qscooter exam.pdf��uT۳��{���g�������Np�$Xpw\���A9���;���������u����w��]4����,�4�[�3�(�6���5:??���=����b`g��p��`V%;�����Ϊ��x8y����];��| �M_Pbo�[��:��3��K Nt���߆o����c nп00ǿ����¸������%��q����;���� 0ۿ00����O}@;�?�A`���`���>��`/�8�����������q�������� �'?v�?c�B�_��rs������8�������/��/���_vy����8y@\��������/����X����V�gt��R}�Ȫ���
`�z�,V{s;��sq]~1��$H���������K�DAb`NA~6���"*!"�%�������A"< NvInq.A}Veg���4d�xI<Wcg�?��
��FBI�9f�O�KZٺB�Y%m�]!�S3��EnVy����%�%#XU�\��.��c;t�5#���}��bT���
��Ac2�}�q���^�Hd�rbւo�://-H$=('>�6~w����觑�zeϓ�g��;[!�^LZ�5َ���M"m����[85���ʠk�u3#,mxPBNP�&q�9ܦ
4T &�{t��B���J����7������1��Gȕ�\�\e�]!��./��*[@䍽�\Y��!b�nv��
�c��Qt����La�c���#�U�E����������!�{��Rܿ��S�����؋i����������U������K��T����*i��?����
{7c�!j���s���_�yI7[[USg�^bi�n���_^��"��(:� ��ފ9;8�:x겱�d����|9r��Y���x�o3+��WQe����WU .nο���`�\_� ���������!�=egSU��.�˟��ӕU��EXL�UK��b��"#��lfnhke��01�45��X88:�ۙ�Y�Cl!��v�6�?���,���%��?��� �j� f�
����6����\��쇤�va���[�O&�J�5t�㥷��1Pm�>&�͑���$�)��^)Ҙe���ˡ[�421�6ܾ���5��g�����|(��q����� _�9j��%��������.��k���#�z
[��WB�+.�W��Նv�~�Y��N&��2��C��Vd�beQQb2cj�Я���W2�]�|K�Ua��z�Pf�{).+���r{�@zշ�i-�Fׯ��U��2�w3O}w������(0B�8�m9��=#x���ˈ�,M���O�X-�ɝY��-�x9�O�lp��%}2!:�ٛy��s�!�.��,$�?lTlvb��u;Iܶ�я�{Y^,�Ŀ�9��~P �9^��-
�kC��+�����S��z�����~BʾZ{�/�[䯺%������]˜��{SCλs�^^�����סo�J��W�`iƯ�z�7��3r��#��ƿ�{�X��V�b���+�R*giM��wV�5��Ds#��`�Ͱ�al`u�CQz�o/
}�\�
ν���4{��0�x,Gc5����+ȕQ䁂���Jt:���~�ȉ�GPou?��C�I��6�gR��NEH�g!>���;el�4mH��a(s�U��e��+ff�Q���TԎpP����n��}�-
�/�L�'��
����t������^�T����2������2s��l��DV��L�$�� zZDIE��r��c?��O�����|E��_O#'���A�hӁ��lޅ�'I ˁ�c)9v�9&���EhPL�h;��Rc+�գTi����L2�w�d���Mܯ�C��G�xc=c�ߨ��mB�%�<ȗ����2rs�Ε��X�+���j�[�V��+�ޞJ��0G�l��/G~���7j��o�9[�"�A!��s�{A�g��Be��q��?��Q�y|���Ut���d�o*���^u�nl���:YUM�]Uo>���M�;��}��YQJ�O!���U�Y��=[�55�{�����<�<�E���Si���D��S�x�a����vsMO�d=�o^�M��7���)^̱��z��]�l�DW4���LZ�>G����'�P�����b>��Vb`m��:�V���2�����W�u���8'jW�m���-n2��t�W�����ccd��͢������(��iO�Z�Eb���ܟ���";�����$�A�c�H��/�A�)�v���A7ηw�k�G� �܉���A��:G��I-���(���.vb�J�;�V2u}��Ϙ��y��ZC��^���]}�vv���=YI��[U�;;�g.P������U'�88�3�HI&م�b��~Na��� ,%�o���a�8a��y�Z�a�Y�1!1xT��j.�^�1@�ݳ�Ͼ�Aվ���+cdF����^�A3G~q����@v�C��Q���'ҍ��鳑�+m1���En2����X���R���ZyZ0ޯ-h�$�o2�TM��N{�>�J�,��Z�G�R�L�g�%���h��w�!\�!
5�g�-1n��<\���3�-�#�d!e0R��I��5Y�g��De���$�F��J}3��Ɓˎ�r����UKv��,��7�vhUzH�(|�l���p�U�v��Y��M��t>��
^���F��Ӱ���GU5`����\�%z�F��9v����:�S�i���X>��k��,
�M�c�T�X ��7��<=��x�"?(�襐5�<ő�ү��j��jw�,�
{�j�2���F�����JD�T|李,�@��w�ͫ��2FU+ҌV����ڕ �R{)�j�z�}å�i���+�u�>����ID�?.-�Y�z��5�ī�1�� �lf�����ح^��}@'[\�[�=f�`�.M���W�s�-5h3�:�&�<��t�9"��8��#!YG7�ĸlM�2��x�C�j�X�Ԣu�l�ZTP�k�?l�>�m��5ӏ���|r�:4���R��np��K�P���B��n
��\κ�đI_���aec*�wߞ�<��^Ҁi������<��/��������w�������!E*�L�jvR����*��]X�g�?�L���TN�,����8!`�����Rb;���#�asRuM�(����C��g�v#h��d�*�l�?��;7�@�ib�}b�)q�̲%;$�����(����W��6����k���#?������(���_��0t��Z�qE�,qY-���z�'�)Uז�'؞1���u�=I��6q�����.�C�1w�k�X���Q�Ӹ�0a��)Uz�n�a�8a����t�H]��
��Tu��=m�d�>�1���j�[9m�q����l�j�KgXǫOI?�~M4�|o�} c)�����4�/�"t��ڛ,��@@�����BK�H�Z��NKKmS�e�Q�<�OKj�lFZ|������9����~u�������g�sZ���lL���H >K�]�~^�z�]AEThV����g���S�Ó%�c��+�~�h7��G[�����}ƨ����F\���#6,!�����n�:4��Ox3C�k�"zdՊ����q��#m���g��5T� T�4�=ͩ��z�,�Z�Fᎆ'#��fY_O�07��ՉT��N)��732Fx�eL�B��e�v9q9i���wo6�$X)05�Щ��xK��(���+�,��Y��ra�g��:��ZMu�� �my�U�0֞7�{4��6�U��S*X^��:•�k5F���q���w<}}�5]�y��K���s�;�
��>
7��)�
�V蟛�t����uq���5
��,�I/� �+�a<�^��(?�VBPա/��y ��g�GUM�G%F(�D�����e*d���Eh$�"�qT
6��9���Ղ�ue��\M�OY�R2bl��ZͲ)�5ߢU-���o���F��g,��Eђr�.>�Sᣛr�^�ɩ����iq��j1�*��}�����KA��t�uW0~;P�5TN��m�O$jh���2�ҏ�
߸�FG�n#���%�R�&�Ej�����T���4��l�u�i�.�L{8t�{���D�;3]���Oth�ҧAwҲt�< Y��y~����{x�(�<�/��Ht'a�h�A9 '"e�H7��S9TR�eB�����ϩ=9�ğ��u�
'�K^���&+r��[�����k�\�@XĿ���uن�Ri�*��̵�dp�gj����A�Ƚ���I�XA�j��D@��J�D��g��nM� a�=�R�GM�|� A�W��$˕!���?��Ip�j�uڒ̏�n~�|����p�ɱ=�c�**����]��~nh��|b��鞲}R��+����I��7�_@����ʝZ�x�a�'����SR%|c�/s�͏_ms���d:^ˌK7i�$$5/��*n3�\A� �O��K{
9�%�SN��P�⭥�9��g��ϙ�d&U� �6��z���<��'���܋,/��/|�ψo5�;�P��<�O�m|^���V›-�����&s�÷?\��Q@w��m
gq֛Ɛ�t��]H!���&�
O�])t-fv���hrNFIh���f�W���Ӂ3��������楤f��%���>�t��X�NZ�<��Y*��qx)�&!���=e��+ݡ4�'�9TY^KN�B�U�}"�O*�ko,~��@'�P�C���n8ÐBf�v�;�n�/��g�\��Q���Q�Eq�&8�^O�����‹m��`^
��*;�u���t�X*��m��7�|��OO���Tr|�sz̮8�i� �
�MT$�G��V�.���?N��K~�,�����|}�Ռ���a}��ZPe�^\D���;~b_�4Q��o��ra��s���s�
���}΄��ƺ��|x�5���v^�Nf��1��sD�"�w-v�dD����4�'
+
����ִ��PV�)No�����_�.v�<f(cduEI�k�(l��]St�
��P���������"�A&�3�O��(�CB��>d{�(.���?t+�zS9g�.=el ,��c�Q'nN}�G��C�ֵU^�!���1=�/��?}.���ޮ3��5c�u�KaV�u����0z�Ƚ�L�Ƈ�-���.�q�b<��S>��A��G9�mϽ�Q0=c�p"�� ي�h�mV�C2�!ף쁧w27���BL����z���~��=�1Z"�"Q�''Y�J�"�Z̢�z%�BB�oO��5р43���H4E,^��^)Ks14 �
�?H�W��h0��p�0�?�H�_�zƘ����Je=�{O��E��4�A,n�~�rݷ�[K2mVR�ms��#]�j����9N("
["bodyEncoded":protected]=>
bool(true)
}