curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$this,'readHeader')) not working

php curl post
php curl headers
curlopt_httpheader php
php curl get response headers
php curl_exec

I have this wrapper to load a symfony project from within Joomla

class NZGBCComponentHelper {
    function requestAndFollow($path = '') {
        $c = 0;$first = 1;$httpcode = 0;
        $uri = JRequest::getVar('uri');
        while ($c <= 4 && ($first || $httpcode == 302)){
            $first = 0;
            $finalSfUrl = NZGBCComponentHelper::buildRequestUri($uri, $path);

            $ch = curl_init($finalSfUrl);

            if(JRequest::getMethod() == 'POST' && $httpcode != 302){
                curl_setopt($ch, CURLOPT_POST, 1);
                $postThrough = array_merge(JRequest::get($_POST),array('_csrf_token' => $_POST['_csrf_token']));
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postThrough));

            // Get sf content
            curl_setopt($ch, CURLOPT_REFERER, JURI::getInstance()->root().$path);
            curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'readHeader');
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_COOKIE, 'symfony='.$_COOKIE['symfony']);

            $return = curl_exec($ch);
            $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

            if (!$return){
                $error = curl_error($ch);

                JResponse::setHeader('Set-Cookie', $mainframe->sym_headers['Set-Cookie']);
            if ($httpcode >= 400){
                $return = "There was an <!--$finalSfUrl-->  error ";
                mail ('', 'symfony wrapper error',

            }else if ($httpcode == 302){
                $query = parse_url(trim($mainframe->sym_headers['Location']), PHP_URL_QUERY);
                if ($outside == 'true'){
        return $return;


    if (!function_exists('readHeader')){
    function readHeader($ch, $header){

        $mainframe =& JFactory::getApplication();
        if($pos = strpos($header, ':')){
            $mainframe->sym_headers[substr($header, 0, $pos)] = substr(strstr($header, ':'), 1);

        return strlen($header);

requestAndFollow is statically called. How can I refer to readHeader() instead of putting it into the global function space. I tried:

curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$this,'readHeader'));//didn't expect this to work - no $this when statically called

curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('NZGBCComponentHelper','readHeader'));

curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(self,'readHeader'));

curl_setopt - Manual, curl_setopt ( resource $ch , int $option , mixed $value ) : bool. Sets an option As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. curl_setopt($curl, CURLOPT_COOKIELIST, $cookie_line); CURLOPT_HEADERFUNCTION is for handling header lines received *in the response*, Description ¶ curl_setopt_array (resource $ch, array $options) : bool Sets multiple options for a cURL session. This function is useful for setting a large number of cURL options without repetitively calling curl_setopt ().

This should work:

curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this,'readHeader'));

curl_setopt_array - Manual, curl_setopt_array ( resource $ch , array $options ) : bool curl_setopt_array($ch, array_merge(array( curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); You can use CURLOPT_HEADERFUNCTION with a callback inside an object. Parameters. ch. A cURL handle returned by curl_init(). option. The CURLOPT_XXX option to set.. value. The value to be set on option.. value should be a bool for the following values of the option parameter:

are u getting ant error like curl_init() not defined?

Enable display error and see if you can get this message.

If you get this message then the cause that CURL is not installed properly.

curl_setopt, () function will set options for a CURL session identified by the ch parameter. The option parameter is the option you want to set, and the value is the value of the option given by the option. Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for your enterprise; Talent Hire technical talent

No need to pollute global namespace:

curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'NZGBCComponentHelper::readHeader');

CURLOPT_RETURNTRANSFER, :- which is used to set the Returntransfer enable for Curl. CURLOPT_POST:- which is used to set the method of posting the variables for Curl. CURLOPT_POSTFIELDS:- which is used to set the variables for Curl. curl_setopt ($ ch, CURLOPT_HEADERFUNCTION, array (& $ this, ‘readHeader’)) no funciona Tengo este contenedor para cargar un proyecto de Symfony desde dentro de Joomla

CURLOPT_POSTFIELDS, is explicitly set to NULL then libcurl will get the POST data from the read callback. CURLOPT_HEADERFUNCTION: A callback accepting two parameters. The first is the cURL resource, the second is a string with the header data to be written. The header data must be written by this callback. Return the number of bytes written. Result:

How to use cURL with PHP - Basic Example, library and a command line tool (like wget) that helps you send files and also download data over HTTP and FTP. It supports proxies, you can transfer data over SSL connections, you can set cookies and even get files that are behind a login. Curl has a built in option for this, called CURLOPT_HEADERFUNCTION. The value of this option must be the name of a callback function. Curl will pass the header (and the header only!) to this callback function, line-by-line (so the function will be called for each header line, starting from the top of the header section).

PHP: Example of usage CURLOPT_HEADERFUNCTION, 17. 18. 19. 20. 21. <?php. $headers = array ();. $ch = curl_init();. curl_setopt( $ch , CURLOPT_URL, '' );. curl_setopt( $ch  I have the following code. do_curl() has been working and still works properly. I recently tried to add the ability to do something with the headers. Thus

  • how about NZGBCComponentHelper::readHeader ??
  • edit your title to Using wrapper to load a symphony project whitin Joomla ? you will get faster respond
  • @Syncmax: I got a working wrapper based on what was out there. The problem is specific to calling this function statically. I got a workaround as well.
  • $this doesn't exist in static calls.
  • Hey I too am having the same issue. The readHeader method is not called. I have a class wherein I am calling the curl_exec() method and the method readHeader is defined globally. But the error is still persistent. Can you provide an example?