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

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

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.

Related

PHP CURL SSL Connecting to XML Gateway

This has been driving me crazy for 2 days - I have 2 PHP functions as detailed below both using curl. They point at exactly the same 'XML gateway' the only difference is one is trying to do it over SSL and the other over unencrypted HTTP.
The HTTP connector operator works exactly as expected, posting the XML file and returning the server response.
The SSL connector returns the ever so vague 'An internal server error occurred. Please try again later'. Nothing shows up in my lighttpd error log, has anyone got any bright ideas?
I'm wondering if it's my web server config/openSSL config. They are both Debian Wheezy standard packages. I appreciate SSL_VERIFYPEER & HOST being set to sale is insecure, however I've been trying to exhaust the options.
openssl s_client -connect xmlgw.companieshouse.gov.uk:443 -ssl3
and the command line function
curl https://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway
also works as expected on the web server.
PHP functions:
//SSL post function
public function getSSLCurlResponse($xml) {
$url = "https://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
//HTTP non SSL function
public function getCurlResponse($xml) {
$url = "http://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
any help would be really appreciated!
Thanks
I have concluded this was an error in the overall connection to the server - though i could not find any way of proving that. I have managed to find an alternative solution without using an SSL socket.

how i can use a specific ip of vps for sending curl request in php [duplicate]

Hi I have a server which has several virtual hosts set up on it.
I wanted to make a curl request to this server's ip using php.
Also I wanted to make this request to a specific hostname on the server's ip.
Is there a way to do it?
A bit more elaboration :
I want to make a curl requests between my servers using internal LAN, using their internal IP. The issue is that I have several sites hosted on this server. So when i make a curl request to the internal IP of the server.. something like (curl_init(xxx.xxx.xxx.xxx)), I want to be able to be tell apache to go to a particular folder pointed to by a virtual host. I hope that made the question a bit more clear.. – Vishesh Joshi 3 mins ago edit
You can set the host header in the curl request:
<?php
$ch = curl_init('XXX.XXX.XXX.XXX');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: subdomain.hostname.com'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($ch);
For HTTPS sites use CURLOPT_RESOLVE which exists in every PHP version since PHP 5.5.
<?php
$ch = curl_init('https://www.example.com/');
// note: array used here
curl_setopt($ch, CURLOPT_RESOLVE, array(
"www.example.com:443:172.16.1.1",
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
Sample output:
* Added www.example.com:443:172.16.1.1 to DNS cache
* Hostname www.example.com was found in DNS cache
* Trying 172.16.1.1...
Base on Leigh Simpson,
It works, but I need query string attach with it.
That's what I work around:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://xxx.xxx.xxx.xxx/index.php?page=api&action=getdifficulty");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: subdomain.hostname.com'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($ch);
?>

cURL request using socks5 proxy fails when using PHP, but it works through the command line

cURL + proxy noob here, having a hard time. I'm having trouble trying to retrieve a web page from a remote secure server via a proxy. Everything has apparently been set up correctly by a remote dev, such that the following command line instruction works and returns what we're looking for:
curl -k --socks5-hostname localhost:xxxx https://hostname/
However, the following PHP does not echo the requested webpage. Instead it echoes the error 'Couldn't resolve host name':
$proxy = 'localhost:xxxx';
$url = 'https://hostname/';
//$proxyauth = 'user:password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
$curl_scraped_page = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error)
echo $error;
elseif ($curl_scraped_page)
echo $curl_scraped_page;
If the $url is changed to a public page, such as Google, the request is successful and everyone is happy.
The connection requires an SSH tunnel if that changes anything at all. The tunnel is open and functioning, as proven by the command line request succeeding.
Is there something obvious that is being missed here?
You need to set option CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5_HOSTNAME, which sadly wasn't defined in old PHP versions, circa pre-5.6; if you have earlier in but you can explicitly use its value, which is equal to 7:
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
In the option CURLOPT_PROXYTYPE you need to set CURLPROXY_SOCKS5_HOSTNAME option instead of CURLPROXY_SOCKS5.
In this case, the DNS query (for hostname resolving) will be sent to SOCKS proxy and not resolved in the local network.
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
This constant available since PHP 5.5.23 and PHP 5.6.7 and cURL 7.18.0, so you can simply use it.

Plesk API and cURL doesnt work

I have a script wich uses the Plesk API to create FTP accounts. The script worked perfectly untill we moved to new VPS at a new provider.
The script doesnt work anymore and I get an error like this:
cURL error number:7 cURL error:Failed to connect to xx.xx.xxx.xxx permission denied.
It seems that cURL doesnt work. What i've tried is changing the php support from fastCGI to Apache module in PLESK and the error message is gone but the PLESK API does nothing though the load time of the page is much longer then when I'm using fastCGI so it looks like it does something.
I also added port 8443 to the firewall for incoming and outgoing.
I'm using PLESK 11 and my script looks like this.
// Plesk login gegevens
$host = "**********";
$login = "**********";
$pass = "**********";
$port = 8443;
// Maak de FTP map aan
$data =<<<EOF
<packet version="1.6.3.5">
<ftp-user>
<add>
<name>John</name>
<password>Doe1234</password>
<home>/private/John_Doe/</home>
<create-non-existent>true</create-non-existent>
<webspace-id>1</webspace-id>
</add>
</ftp-user>
</packet>
EOF;
sendCommand($data, $login, $pass, $host, $port);
function write_callback($ch, $data)
{
// echo $data;
return strlen($data);
}
function sendCommand($data, $login, $passwd, $host, $port=8443)
{
echo $data;
$script = "enterprise/control/agent.php";
$url = "https://$host:$port/$script";
$headers = array(
"HTTP_AUTH_LOGIN: $login",
"HTTP_AUTH_PASSWD: $passwd",
"HTTP_PRETTY_PRINT: TRUE",
"Content-Type: text/xml",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, &$headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'write_callback');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$result = curl_exec($ch);
if (!$result)
{
echo "\n\n-------------------------\ncURL error number:".curl_errno($ch);
echo "\n\ncURL error:".curl_error($ch);
}
curl_close($ch);
return;
}
I am not plesk or linux person, But i would like to contribute my part with this discussion,
I have a script wich uses the Plesk API to create FTP accounts. The
script worked perfectly untill we moved to new VPS at a new provider.
This statement clearly says problem is not your code, It's with your VPS.
Problem maybe!
1) Client permission to update Parallels Panel via API-RPC, try with admin credentials.
Issue found : plesk 9.2. So it may be fixed in next release but i am not sure. Source
2) Check SElinux is disabled or not, if not disable it and check. Your next question must be, how to disable SElinux. Answer is here well documented
3) You should install mod_suphp and follow our tutorial and will work. Check out here for more.
I just above summarized, few thinks hope it helps.
i am not sure but see below URL I think it is very help full to you.
Unusual HTTP headers in the Plesk API
According to Unusual HTTP headers in the Plesk API by TRiG
Or Read it
Below Answer by rdo according to above URL
Plesk uses own headers. In custom http request you can add any valid headers for example some webservers add own header like 'powered by: xxxx', so it's ok.
pretty print header is required for pretty xml output.
HTTP_AUTH_LOGIN header contains panel user login. HTTP_AUTH_PASSWD header contains panel user password. CURLOPT_USERPWD is not required.
try to use these options: $ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_POSTFIELDS, $packet);
curl_setopt($ch, CURLOPT_TIMEOUT, 1200); //wait 20min
$response = curl_exec($ch);
Assuming you remembered to change the host, was your plesk user and password recreated after the move? Try logging into plesk on the VPS directly using the credentials from your script to make sure the account is ok.

How to set hostname using php curl for a specific ip

Hi I have a server which has several virtual hosts set up on it.
I wanted to make a curl request to this server's ip using php.
Also I wanted to make this request to a specific hostname on the server's ip.
Is there a way to do it?
A bit more elaboration :
I want to make a curl requests between my servers using internal LAN, using their internal IP. The issue is that I have several sites hosted on this server. So when i make a curl request to the internal IP of the server.. something like (curl_init(xxx.xxx.xxx.xxx)), I want to be able to be tell apache to go to a particular folder pointed to by a virtual host. I hope that made the question a bit more clear.. – Vishesh Joshi 3 mins ago edit
You can set the host header in the curl request:
<?php
$ch = curl_init('XXX.XXX.XXX.XXX');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: subdomain.hostname.com'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($ch);
For HTTPS sites use CURLOPT_RESOLVE which exists in every PHP version since PHP 5.5.
<?php
$ch = curl_init('https://www.example.com/');
// note: array used here
curl_setopt($ch, CURLOPT_RESOLVE, array(
"www.example.com:443:172.16.1.1",
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
Sample output:
* Added www.example.com:443:172.16.1.1 to DNS cache
* Hostname www.example.com was found in DNS cache
* Trying 172.16.1.1...
Base on Leigh Simpson,
It works, but I need query string attach with it.
That's what I work around:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://xxx.xxx.xxx.xxx/index.php?page=api&action=getdifficulty");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: subdomain.hostname.com'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($ch);
?>

Categories