Joomla site on LAMP server behind proxy cannot access HTTP resource stream

旧城冷巷雨未停 提交于 2019-12-24 10:48:08

问题


Setup: 1. Joomla 1.5 website on a LAMP server (CentOS 5.2/Apache 2.2/PHP 5.2.9/mysql 5) 2. Joomla module for currency conversion added. Module uses google finance to convert currency 3. LAMP stack resides in the intranet behind a proxy. The server environment variables for http_proxy, yum.conf proxy have been setup, and kernel successfully updated. 4. phpinfo() clearly shows curl is installed 5. module mentioned in '2.' allows 3 methods to connect to google finance, fread(), file_get_contents() and using the cURL libraries. As the box is behind a proxy, only the cURL libraries method should work.

Problem: on a WAMP stack, the curl library method works fine. On the lamp stack, however, the module is unable to communicate with google finance, and throws an error mentioning connect timed out. Here's some code to make it clearer.

if (isset($_GET['process'])) {        
$url = "http://finance.google.com/finance/converter?a={
$_GET['a']}&from={$_GET['from']}&to={$_GET['to']}";
$app->get_page($url);
$data = $app->process();
}  

function get_page($url) {
if ($url!='') {
echo $url;
$ch = curl_init ();
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                    curl_setopt($ch, CURLOPT_BINARYTRANSFER, $this->binary);
                    $this->html = curl_exec($ch);
                    curl_close($ch);
            }
    }

I even tried adding a curl_setopt($ch, CURLOPT_PROXY,'10.x.xx.xx:8080'); after curl_init(), to no avail. I've compiled apache with libcurl and php enabled, and I need to know the following: 1. How to instruct php to route outgoing requests(streams) through the proxy? 2. Do I need to configure cURL (libcurl) with the proxyname and port? 3. I've switched iptables off, so the linux firewall is not in the picture anymore, is there anything else I need to do to allow outgoing requests? 4. I've setup the proxy so that my LAMP stack is unblocked for all content, cURL works off the command line, but not from php/apache. What am I missing? Any environment variables? Any switches?

Thanks in advance for your time.

Shrinivas


回答1:


Here's an example using a local SOCKS5 proxy on port 1090:

<?php
$url = 'www.whatismyip.com/automation/<your unique whatismyip hash>';

function get_page($url, $proxy=true) {
    if ($url!='') {
        $ch = curl_init ();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        if ($proxy) {
            curl_setopt($ch, CURLOPT_PROXY, 'localhost');
            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
            curl_setopt($ch, CURLOPT_PROXYPORT, 1090);
        }
        $html = curl_exec($ch);
        curl_close($ch);
        return $html;
    }
}


var_dump(get_page($url));
var_dump(get_page($url, false));

You'd probably want to use curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); and curl_setopt($ch, CURLOPT_PROXYPORT, 8080); instead.



来源:https://stackoverflow.com/questions/1476938/joomla-site-on-lamp-server-behind-proxy-cannot-access-http-resource-stream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!