tags:

views:

749

answers:

1

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

A: 

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.

TML
I tried this curl test php script, added if(curl_exec($ch) === false){echo 'Curl error: ' . curl_error($ch);} Hit the script via browser and got a 'Failed to connect to 10.x.xx.x: Permission denied' indicating that curl was unable to contact my proxy, a SYN_SENT status is seen in the netstat o/p for outgoing tcp requests. I don't know what I need to do to get curl and the proxy to pair up, do I need curlwrappers?
Shrinivas
Have you tried using 10.x.xx.x:8080 as the proxy for your web browser to make sure it works?
TML
well, as it is a LAMP box setup with CentOS, no GUI, I've only needed yum and wget to download updates and install apache, php and mysql. I've set an environment variable 'http_proxy' that helps wget get through the proxy, and updated yum.conf with the proxy ip and port, and it works fine too. Curl from the command line picks up the proxy properly and is able to get http://www.google.com/, but curl used in a php script hosted on apache is not able to do so. Phpinfo() shows the Curl module is loaded.
Shrinivas