Answer the question
In order to leave comments, you need to log in
How to force cURL to produce result instead of display?
I was surprised to find that the usual curl_exec displays the result on the screen, despite the CURLOPT_RETURNTRANSFER.
The code:
public function getHTML($url){
if(!isset($this->curl) || is_null($this->curl))
{
$this->init();
}
curl_setopt($this->curl, CURLOPT_URL, $url);
$page = curl_exec($this->curl);
$this->close();
return $page;
}
protected function init(){
$this->curl = curl_init();
$this->setOptions();
curl_setopt_array($this->curl, $this->options);
}
private function setOptions(){
if(empty($this->options))
{
$this->options = array(
CURLOPT_AUTOREFERER => true,
CURLOPT_COOKIESESSION => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPGET => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_ENCODING => 'gzip,deflate,sdch',
CURLOPT_HTTPHEADER => array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.3',
'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
'Cache-Control: max-age=0'
),
CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7'
);
if($this->cookie){
$this->options[CURLOPT_COOKIEFILE] = "cookie.txt";
$this->options[CURLOPT_COOKIEJAR] = "cookie.txt";
}
}
}
ob_start();
curl_exec($this->curl);
$this->close();
$page = ob_get_contents();
ob_clean();
PHP 5.5.9-1ubuntu4.11 (cli) (built: Jul 2 2015 15:23:08)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
PHP 5.5.22 (cli) (built: Feb 26 2015 16:05:53)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
Answer the question
In order to leave comments, you need to log in
Phew. I tried the suggested function - it works. I try mine and it doesn't work. Half the night I thought what was the matter and twisted it this way and that, everything seems to be the same.
I don't know what this magic is, but it turned out that this is how it works:
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
private function setOptions(){
if(empty($this->options))
{
$this->options = array(
CURLOPT_AUTOREFERER => true,
CURLOPT_COOKIESESSION => false,
CURLOPT_HTTPGET => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_ENCODING => 'gzip,deflate,sdch',
CURLOPT_HTTPHEADER => array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.3',
'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
'Cache-Control: max-age=0'
),
CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7'
);
if($this->cookie){
$this->options[CURLOPT_COOKIEFILE] = "cookie.txt";
$this->options[CURLOPT_COOKIEJAR] = "cookie.txt";
}
}
}
My little helper :-)
function sendRequest($url, $fields = [], $method = 'get', $config = []) {
$fields = http_build_query($fields);
// http://php.net/manual/ru/function.curl-setopt.php
$_config = [
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36',
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt',
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => '',
CURLOPT_TIMEOUT => 30,
CURLOPT_AUTOREFERER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
];
if ($method == 'post') {
$_config[CURLOPT_POSTFIELDS] = $fields;
$_config[CURLOPT_POST] = true;
}
foreach ($config as $key => $value) {
$_config[$key] = $value;
}
$curl = curl_init();
curl_setopt_array($curl, $_config);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question