BigIndian66
BigIndian66

Reputation: 27

How to download a generated pdf file with PHP?

I'm struggling to get a PDF file generated by an external server.

Here is the link to the resource : https://www.test.colisprive.com/mcadesk/Externe/ShowEtiquettePDF.aspx/etiquette_colis-23-23000000000833300-PDF_DEFAUT-N/

So as you can see, no identification needed.

I noticed that I can write anything I want at the end of the URL and it will be interpreted as a title by the browser integrated pdf reader. But when using "save as..." the name of the file is already set to a fixed value.

I tried to get it with cURL but it returns "Object moved to here."(link), Except the link doesn't work and using a CURLOPT_FOLLOWLOCATION returns false.

I really need to get to download pdf files from this URL but I'm completely stuck, any idea would be very welcome !!

Thanks, BR,

Manu

edit : I tried this :

$curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_HTTPHEADER => array(
        ),
        CURLOPT_URL=>"https://www.test.colisprive.com/mcadesk/Externe/ShowEtiquettePDF.aspx/etiquette_colis-23-23000000000833300-PDF_DEFAUT-N/Etiquette_23000000000833300.pdf",
        CURLOPT_RETURNTRANSFER => 1,
    ));

    $resp = curl_exec($curl);
    var_dump($resp);

    curl_close($curl);

Upvotes: 2

Views: 1152

Answers (1)

Paolo
Paolo

Reputation: 15827

The mentioned website does not serve the requested content (and issue a redirect instead) if the request does not provide a User-Agent header.

PHP's CURL does not set a User-Agent by default, nor file_get_contents. Differently, command line curl and Python's urllib.request.urlretrieve do, that's why you succeeded with the latter.

With PHP's CURL you have to set the User-Agent by your own but it's just one line.

Note that the website you're accessing requires it, but accepts any User-Agent.

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_HTTPHEADER => array(
        "User-Agent: curl" // <--- the User Agent is specified by setting the corresponding header
    ),
    CURLOPT_URL=>"https://www.test.colisprive.com/mcadesk/Externe/ShowEtiquettePDF.aspx/etiquette_colis-23-23000000000833300-PDF_DEFAUT-N/Etiquette_23000000000833300.pdf",
    CURLOPT_RETURNTRANSFER => 1
));

$resp = curl_exec($curl);

var_dump($resp);

curl_close($curl);

 

The output you get looks like:

%PDF-1.4
1 0 obj
<< 
/Length 1514
/Filter /FlateDecode
.
.
.

 

you're actually receiving a PDF.


You may then serve the fetched PDF

echo $resp;

or store the file on your server

file_put_contents( "/path/to/file", $resp );

Upvotes: 2

Related Questions