Search code examples
phprequest-headerspdf-form

Get Request Header is missing (x-auth-token) and how to return stream format PDF?


I am developing a POST API which calling a service to return url of .pdf file. It returns {url, token and other fields}. And I want to redirect to this url and get the pdf file, but i fail.

example: url : "https://xxxxxxx.com/abc.pdf" token : "hello123"

The url is requiring request headers "X-Auth-Token"

            // post request data
            $SFOrder = [
                'reference_number' => $shipmentReferenceNumber,
                'order_id' => $orderId,
            ];

            // cloud print service and retrieve data
            $result = $this->openSFService->cloudPrintSingle($SFOrder);
            $objArray = json_decode($result['apiResultData'], true);
            $files = $objArray['obj']['files'];    

        
            // token and url
            $token = $files[0]['token'];
            $url = $files[0]['url'];

            $headers = array();
            header("Access-Control-Allow-Origin: *");
            header("Access-Control-Allow-Headers: *");
            $headers[] = "X-Auth-Token: $token";
            $headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';

            $state_ch = curl_init();
            curl_setopt($state_ch, CURLOPT_URL, $url);
            curl_setopt($state_ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($state_ch, CURLOPT_HTTPHEADER, $headers);
            $state_result = curl_exec($state_ch);

            // method 2:
            print_r($state_result);

            // method 1:
            header('x-Auth-Token: ' . $token);
            header("Location: " . $url); /* Redirect browser */            

            exit();

I have tired two ways:

  1. I used header("X-Auth-Token: " . $token) and header("Location: " . $url) redirect the link, but the request headers "x-auth-token" is missing , which returning 404 when I accessed the page.

  2. return stream format like below?? how to convert the content to pdf?

%PDF-1.5 %���� 4 0 obj <>stream ����JFIF��C    $.' ",#(7),01444'9=82<.342��C  2!!22222222222222222222222222222222222222222222222222���"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?O��$Y���OW4|m���]��R��NQ�Et��x���?�V�E���>5��*��H����6��Ϯ�ةo��(袀 G�M��� ��M>�ȱ�ܽ���h��=�_�Z��������< �%��_� ���Z�Ew������V��O�tx�H/���+k��E���?�'����l?�U�z���=������������"E�����m\��2�������(��;����o�������>stream x�������mH@���/� endstream endobj 6 0 obj <>]/Intent/Perceptual/Subtype/Image/Height 70/Filter/FlateDecode/Type/XObject/Width 70/SMask 5 0 R/Length 1832/BitsPerComponent 8>>stream x�ݛ�K_��� �eE��Qh`A� AI�f� ����a�$2��ʢz1��]����C�Y���f�b7sΜ=��x�|�朽�Z�;��Zk�|?e�|���͛���7nljj����'��O��@5*��?|��ybb���{ժU^BЄ�4GH�y���w��۷���&dguuuKKK{{{.��?��"*�� Q,�_�~MNNnذ�4�����������%P�jT� M9�E8*�������Հ�˗���=|����!�R��@Q��G��ݻ��.U���p�ܹ/_��%QD��@JӒ��۷W�\)�x�p�·o߲P�X���P�������G��{������c�*�@�T)`F*�q�mmm"����Ν;������JE;f���?|��m�֭o߾M��D@)�� b��(�677����G�v&��#a�)��~��bF�������1u� '##��0/��І������U����q�&����Sh m�̲x6`��tL-�Qtvv �E�;IA�;18���fκ�� �'���0<<���g�( %�:ɨ���$9����hzz:Zz��q)�r劳 F����)��"K,EQ<���V)���-E�SE�GK%����dԼ��ѯ�7%�]�m*AS...........

Solution

  • $state_ch = curl_init();
    
            $headers = array();
            $headers[] = "Cache-Control: public";
            $headers[] = "x-auth-token: $token";
            
            curl_setopt($state_ch, CURLOPT_URL, $url);
            curl_setopt($state_ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($state_ch, CURLOPT_HTTPHEADER, $headers);
            $state_result = curl_exec($state_ch);
    
            curl_close($state_ch);
    
            $data = $state_result;
            header('Content-Type: application/pdf');
            header('Content-Disposition: attachment; filename="abc.pdf"');
            echo $data;
    
            exit();