I want to use PHP's cURL to visit a page on an external site, and get some the whole html content of the page.
When i visit the site, it will redirect me to another page on the same site. Also, i have to set the useragent, i want a useragent for PC windows7 chrome and iPhone 4s. This is what i got so far:
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_AUTOREFERER , true)
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$kl = curl_exec ($ch);
curl_close($ch);
echo $kl;
I will probably run into more errors.
You might also need to consider urls with https
$cookie = tmpfile();
$userAgent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31' ;
$ch = curl_init($url);
$options = array(
CURLOPT_CONNECTTIMEOUT => 20 ,
CURLOPT_USERAGENT => $userAgent,
CURLOPT_AUTOREFERER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => $cookie,
CURLOPT_COOKIEJAR => $cookie ,
CURLOPT_SSL_VERIFYPEER => 0 ,
CURLOPT_SSL_VERIFYHOST => 0
);
curl_setopt_array($ch, $options);
$kl = curl_exec($ch);
curl_close($ch);
echo $kl;
So:
CURLOPT_FOLLOWLOCATION
as @TroyCheng indicated CURLOPT_COOKIEFILE
& CURLOPT_COOKIEJAR
. Why don't you use a library like Buzz ?
$request = new Buzz\Message\Request('GET', '/', 'http://google.com');
$response = new Buzz\Message\Response();
$client = new Buzz\Client\Curl();
// do not check https validity
$client->setVerifyPeer(false);
// define your user agent
$client->setOption('CURLOPT_USERAGENT', $userAgent);
$client->setOption('CURLOPT_COOKIEFILE', true);
$client->setOption('CURLOPT_COOKIEJAR', true);
$client->send($request, $response);
if ($response->isOk())
{
echo $response->getContent();
// or if you want the dom
echo $response->toDomDocument();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.