PHP locally hosted, connecting to external resources through a proxy server - php

I'm trying to do some locally hosted facebook development but I'm on a university network, and therefore all outgoing connections from my computer need to pass through our proxy server. The main problem I'm having is that I can't seem to find any documentation for setting up apache to USE a proxy server, rather than to ACT as a proxy server.
Upon thinking about this however, perhaps when I do a "cURL" request or an fopen, that apache does not perform the retrieving of data, and it is instead the PHP drivers that do this. Older versions allowed you to setup a global proxy in the PHP.ini file, but not in PHP 5.
I have to use code to actually physically set the defaults and cannot find any config files where I can set them permanently. For example, this sets up streams so fopen can function:
$r_default_context = stream_context_get_default
(
array
(
'http' => array
( // All HTTP requests are passed through the local NTLM proxy server on port 8080.
'proxy' => 'tcp://proxy.munged.edu:8080',
'request_fulluri' => True,
),
)
);
but this will not set everything which is required as to use cURL, I have to do this:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, "http://proxy.munged.edu:8080");
curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
Is there anyone who knows how to set all things that require outgoing connections to use this proxy as I don't won't code that's specific to this computer (because my plan was to work on my code locally and then upload it to some webspace when it's done: the change/upload/refresh cycle is ALOT more time consuming than just that change/refresh cycle)
edit:
just to clarify, i have been including all this in a file called "proxyconfig.php" then checking for it's existance, and include()-ing it at the top. if there's no way to set up the defaults in config files, having the methods to set up all the things that the facebook.php page used for their API requires would be awesom.

Your method is correct, assuming that the application is in iframe mode (FBML applications require Facebook being able to callback to your server).
If the issue is wanting to be able to develop locally and deploy to a remote site with minimal modification to your files, I'd recommend extending BaseFacebook as a new class called LocalBaseFacebook and changing CURL_OPTS to:
public static $CURL_OPTS = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_USERAGENT => 'facebook-php-3.0',
CURLOPT_PROXY => 'http://proxy.munged.edu:8080',
CURLOPT_PROXYPORT => 8080
);
When deploying out, make a switch when instantiating the Facebook class based on hostname or some uniquely identifying property / configuration (you could even use a $_GET variable such as ?is_local=1) and attach that to the end of your Canvas URL.

Related

Why running Ratchet websocket server via php pends browser request?

I'm building an hybrid mobile app with a framework. It's all good and now I want to update views when needed. So I thought about websockets.
After reading Ratchet doc, I've been able to set this server running with command lines. I know about deployment solution for making server run automatically but It's a bit complex for me. So I tried to run the server from php with this following code:
...
$port = 8080;
//echo("Starting server on port " . $port);
$server = IoServer::factory(
new HttpServer(
new WsServer(
$MessageHandler
)
),
$port
);
$server->run();
I make a request http://localhost/app/ws/run-server and it runs the server as expected but the browser pends this request and any others ones called after.
I then must comment this line $server->run() and restart Apache via XAMPP before I can go ahead.
So I really need a help because I've been searching for days and days.
Thanks in advance!
I was in the same position: I wanted to quickly start/stop Ratchet from a web interface without having to resort to hacks that involve exec() or having to install ZMQ.
So what I did was: call the script that kicks off the Ratchet websocket server from cURL:
$options = [
CURLOPT_URL => 'url/to/server.php',
CURLOPT_RETURNTRANSFER => false,
CURLOPT_TIMEOUT_MS => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method
];
// init
$curl = curl_init();
curl_setopt_array( $curl, $options );
// exec
curl_exec( $curl );
curl_close( $curl );
Parts of interest: CURLOPT_TIMEOUT_MS to a really low value, CURLOPT_RETURNTRANSFER to false (not totally sure about that one though). This cURL call will immediately return and will not block the rest of your script execution.
Stopping it is not possible without ZMQ or talking to the webserver layer of the Ratchet Websocket server (not sure how to do that, and if it's possible at all). The script runs forever, unless it crashes. I communicate with the script through WebSockets from then on, also for administrative access like stopping it. But there may be other (better) ways to do that.

PHP : relay resource request through different server

I have the following situation, trying to clearify with a 'scheme'.
On a webapplication, we are showing resources comming from server3. But because server3 is a intranet endpoint, we are loading the resource through some extra steps. Each server has a Apache running to cap the incomming requests.
The infra setup has some 'restrictions':
Server 1 contains the webapplication
Server 2 is only reachable by server1, and can't host the webapplication. And acts as a 'entry' point to server 3, restricted access only for requests comming from server1 (intranet server)
Server 3 (resources that need to be shown on the webapplication) is only reachable by server 2. Server 3 is an intranet server.
As you see on the scheme above, loading a resources takes some steps. I'm wondering if there are any better ways within php to stream contents from server3 through server2 initiated on server1. Without having to transport the content on each step.
I've been looking at file_get_contents in combination with stream_context_create. But not sure if this is possible relaying the request through a server in the middle.
I've tried something like this (stripdown):
$opts = array(
'http' => array(
'timeout' => 10,
'proxy' => 'tcp://server2:port',
'request_fulluri' => true
)
);
$context = stream_context_create($opts);
$data = file_get_contents('server3/files/footer.png', false, $context);
echo $data;
Any expertise is welcome.
Thanks!

cURL - Protocol https not supported or disabled in libcurl (post PHP update)

I know there are dozens of questions about this particular topic, but the usual suggestions don't really seem to help. I have checked the server under the root account and the specific account I'm working with and in both cases (if they even can be different) SSL is listed as a feature and HTTPS is listed as a protocol.
My cURL functions were working fine until yesterday when we upgraded PHP from ~5.1/5.2 to 5.4.26. My assumption was that PHP and/or cURL were compiled without SSL support, but that doesn't seem to be the case.
If it helps, the functions are calling Appcelerator's cloud services. This is one of the login functions, the first to throw the "trying to get property of non-object" error because $res is false:
function login() {
$url = 'https://api.cloud.appcelerator.com/v1/users/login.json?key=<MY_APP_KEY>';
$options = array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => array(
'login' => '<MY_APP_LOGIN>',
'password' => '<MY_APP_PASSWORD>'
)
);
$curl_session = curl_init($url);
curl_setopt_array($curl_session, $options);
$res = curl_exec($curl_session);
curl_close($curl_session);
$this->session_id = json_decode($res)->meta->session_id;
}
Is it possible that even though SSL and HTTPS are listed that they're not actually in effect? Is there a way to check and if necessary fix that?
One possible problem is that you have more than one libcurl version installed in your machine, so your checking as root returns the info about a global installation while your PHP environment runs a different version.
In your PHP program you can instead run curl_version() and see what it says regarding SSL support and versions etc.

loading remote data on server in PHP

I am trying to load content from a remote URL in my php code. There are two restrictions:
I need to use the dedicated server IP I have so the REMOTE_ADDR of the other server has to be my dedicated IP. This eliminates Curl because Curl uses a proxy to load the remote URL and the proxy changes the IP address which does not work.
I need to load the data on my back-end using PHP. I do not wish to use Javascript for security reasons.
Are there any other solutions other then Curl?
Thank you
What about file_get_contents($url)? Just note that some websites require a user-agent, so you'll have to set one with ini_set() before making the call.
When using the stream api and a wrapper that utilizes the socket-wrapper you can set the bindto context parameter to accomplish (1) :
Used to specify the IP address (either IPv4 or IPv6) and/or the port number that PHP will use to access the network. The syntax is ip:port. Setting the IP or the port to 0 will let the system choose the IP and/or port.
$ctx = stream_context_create( array(
'socket' => array(
'bindto' => '192.168.0.107:0',
)
));
$c= file_get_contents('http://php.net', 0, $ctx);
You could probably use fopen for this.
Have you tried file_get_contents
<?php
$homepage = file_get_contents('http://www.stackoverflow.com/');
echo $homepage;
?>

cURL - Operation not permitted error

I'm trying to use cURL with PHP and its giving me this error:
"Failed to connect to 208.77.188.166: Operation not permitted"
I'm pretty sure its a server issue - but just in case, here is my code:
<?php
$ch = curl_init();
$url ="http://www.example.com";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch) or die(curl_error($ch));
echo $output;
?>
cURL is enabled on Apache, I've tried changing permissions of the file to 777.
Any ideas?
It's possible that you need to enable allow_url_fopen (reference) -- you can do this in an .htaccess file if it's on apache.
You can enable this by putting this in an .htaccess file:
php allow_url_fopen on
Make sure you set all required CURL options:
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => "spider",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
I will also suggest you echo out errors also using something like this:
$hostconnect = curl_init($url);
$errmsgcurl = curl_error($hostconnect);
echo $errmsgcurl;
The above code is not tested and it just serves as an example.
Also would suggest trying out your code on a local apache server this way you can tell where the problem sits easily.
Many shared hosting providers prohibit outbound connections. Bluehost, for example, requires that you purchase a static IP before allowing outbound connections. Then you need to make sure CURL knows what outbound interface to use.
The error you are receiving is most likely do to a firewall blocking all outbound connections. Many shared hosting providers are blocking outgoing port 80 connections to try to stop rampant errors in PHP scripts that allow remote includes to then be used as an attack vector against the server.
Please contact your host, and if this is the case you will need to find an alternate way to connect to the remote host, or move hosting companies.
You should try using a version of curl installed on the server or on your workstation (command line version) and try to replicate the error, you may need to set a referrer header in the curl request, but that all depends on the server you are trying to contact.
Could be a proxy issue or some kind of authentication problem on the server - can you access this URL using a regular web browser ?

Categories