V
V
Viktor Koreysha2015-11-10 17:24:34
PHP
Viktor Koreysha, 2015-11-10 17:24:34

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";
            }
        }    
    }

There was no way to think. I inserted this crutch:
ob_start();
curl_exec($this->curl);
$this->close();
$page = ob_get_contents();
ob_clean();

But when transferring the project to the production server, it turned out that curl_exec no longer displays it on the screen, but again you need to turn on the old version.
Question two:
How to make what would work both there and there?
What did I do wrong?
PS Versions of php
on the first server:
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

on the second:
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

2 answer(s)
V
Viktor Koreysha, 2015-11-11
@Iktash

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,

not in reverse order:
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,

The order of the other options doesn't seem to matter. What kind of magic is not clear. Moreover, as I wrote at the beginning on one server, it works this way and that way, and on the other there is such a thing.
As a result, the working version of setting options looks like this:
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";
            }
        }    
    }

Can someone explain what's the point here?

D
DireX, 2015-11-11
@xDireX

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 question

Ask a Question

731 491 924 answers to any question