franek_dziupla
franek_dziupla

Reputation: 1

Force curl to download page as browser

How to force curl (with) PHP to download page as browser? The page I want to download is a price comparator, for e.g. http://www.ceneo.pl/22416171. It's public, anybody can access site.

To check if the curl downloading is even possible, I typed on my Debian-based local server

curl http://www.ceneo.pl/22416171

And it worked perfectly. But I do need to use it on my Virtual PHP-Apache serv, so I need to use PHP to do it.

While trying to download page as PHP-based curl, it gives me nothing, opposite to shell curl. Why? How can I get the right content on PHP?

Tried:

                        <?php
                        $curl = curl_init(http://www.ceneo.pl/22416171);
                        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                        curl_setopt($curl, CURLOPT_HEADER, 1);
                        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($curl,CURLOPT_HTTPHEADER,
                        array(
                        'User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:28.0) Gecko/20100101 Firefox/28.0',
                        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                        'Accept-Language: pl,en-US;q=0.7,en;q=0.3',
                        'Accept-Encoding: gzip, deflate',
                        'p3p: CP="NOI CURa ADMa DEVa TAIa OUR BUS IND UNI COM NAV INT"',
                        'Vary: Accept-Encoding',
                        'Content-Type: text/html; charset=utf-8',
                        'Cache-Control: private'
                        ));



                        $body = curl_exec($curl);
                        curl_close($curl);

                        echo $body;
                        ?>

I tried also to use

<?php exec(curl http://www.ceneo.pl/22416171); ?>

But it gave

curl: /usr/local/lib/libcurl.so.4: no version information available (required by curl)

Upvotes: 0

Views: 978

Answers (1)

Latheesan
Latheesan

Reputation: 24116

Take a look at the documentation: http://www.php.net/manual/en/curl.examples.php

This is how you do it:

test.php

<?php

// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "http://www.ceneo.pl/22416171");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//set headers
curl_setopt($ch,CURLOPT_HTTPHEADER, array(
    'User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:28.0) Gecko/20100101 Firefox/28.0',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language: pl,en-US;q=0.7,en;q=0.3',
    //'Accept-Encoding: gzip, deflate',
    'p3p: CP="NOI CURa ADMa DEVa TAIa OUR BUS IND UNI COM NAV INT"',
    'Vary: Accept-Encoding',
    'Content-Type: text/html; charset=utf-8',
    'Cache-Control: private'
));

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);

// debug
echo $output;

Demo of it working (only the html output from the site is retrieved):

enter image description here

Upvotes: 1

Related Questions