【问题标题】:curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$this,'readHeader')) not workingcurl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$this,'readHeader')) 不工作
【发布时间】:2011-09-22 20:22:35
【问题描述】:

我有这个包装器来从 Joomla 中加载一个 symfony 项目

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

            if($this->headers['Set-Cookie']){
                JResponse::setHeader('Set-Cookie', $mainframe->sym_headers['Set-Cookie']);
            }
            if ($httpcode >= 400){
                $return = "There was an <!--$finalSfUrl-->  error ";
                mail ('jd@automatem.co.nz', 'symfony wrapper error',
                $finalSfUrl."\r\n".
                $httpcode."\r\n".
                $return
                );

            }else if ($httpcode == 302){
                $query = parse_url(trim($mainframe->sym_headers['Location']), PHP_URL_QUERY);
                parse_str($query);
                parse_str($uri);
                if ($outside == 'true'){
                    JApplication::redirect(trim(urldecode($uri)));
                }
            }
            curl_close($ch);
            $c++;
        }
        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 是静态调用的。如何引用 readHeader() 而不是将其放入全局函数空间。我试过了:

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'));

【问题讨论】:

  • NZGBCComponentHelper::readHeader 怎么样??
  • 编辑你的标题为使用包装器加载一个带有Joomla的交响乐项目?你会得到更快的响应
  • @Syncmax:我得到了一个基于现有内容的工作包装器。该问题特定于静态调用此函数。我也有一个解决方法。

标签: php curl joomla header


【解决方案1】:

看来这可以正常工作

  • libcurl 7.19.7、Ubuntu 10.10、PHP 5.3.2

但不在

  • Debian Lenny 上的 libcurl 7.18.2,PHP 5.2.6

两个版本的解决方法是将标头函数放在全局空间中

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

【讨论】:

    【解决方案2】:

    这应该可行:

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

    【讨论】:

    • $this 在静态调用中不存在。
    【解决方案3】:

    您是否遇到了诸如 curl_init() 未定义的 ant 错误?

    启用显示错误并查看是否可以收到此消息。

    如果您收到此消息,则说明 CURL 未正确安装。

    【讨论】:

    • 嘿,我也有同样的问题。不调用 readHeader 方法。我有一个类,其中我正在调用 curl_exec() 方法,并且 readHeader 方法是全局定义的。但错误仍然存​​在。你能举个例子吗?
    【解决方案4】:

    无需污染全局命名空间:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 2013-11-19
      • 2016-05-15
      • 1970-01-01
      相关资源
      最近更新 更多