Skip navigation

Category Archives: PHP

Most of the time Htaccess and Apache can handle proxies like this much easier, but just in case you need to do this, here is the code for a PHP proxy from http://developer.yahoo.com/javascript/samples/proxy/php_proxy_simple.txt

<?php
// PHP Proxy example for Yahoo! Web services. 
// Responds to both HTTP GET and POST requests
//
// Author: Jason Levitt
// December 7th, 2005
//

// Allowed hostname (api.local and api.travel are also possible here)
define ('HOSTNAME', 'http://search.yahooapis.com/');

// Get the REST call path from the AJAX application
// Is it a POST or a GET?
$path = ($_POST['yws_path']) ? $_POST['yws_path'] : $_GET['yws_path'];
$url = HOSTNAME.$path;

// Open the Curl session
$session = curl_init($url);

// If it's a POST, put the POST data in the body
if ($_POST['yws_path']) {
	$postvars = '';
	while ($element = current($_POST)) {
		$postvars .= key($_POST).'='.$element.'&';
		next($_POST);
	}
	curl_setopt ($session, CURLOPT_POST, true);
	curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
}

// Don't return HTTP headers. Do return the contents of the call
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// Make the call
$xml = curl_exec($session);

// The web service returns XML. Set the Content-Type appropriately
header("Content-Type: text/xml");

echo $xml;
curl_close($session);

?>

More Curl Code

This shows how to use curl a little simpler..


>?php
/*  
         _   _ ____  _
     ___| | | |  _ \| |
    / __| | | | |_) | |
   | (__| |_| |  _ <| |___
    \___|\___/|_| \_\_____|


01/08/2008 By AskApache

http://www.askapache.com/security/curl-google-post-feed.html

This script will login to google reader with the $username and 
$password variables and fetch the number of subscribers for 
the $feedurl variable that you specify.

DEMO: http://www.askapache.com/online-tools/curl-google-feed/
*/
 




/****************************************
   SETTINGS
****************************************/
// if($_SERVER['REMOTE_ADDR'] !== '1.1.1.1')die();// only allow IP 1.1.1.1
$username=urlencode('youremail@gmail.com');
$password="yourpassword";
$feedurl=urlencode('thefeedurl'); //http://feeds.askapache.com/apache/htaccess




// create cookie file
$google_cookie=tempnam("./","XX");

$url="http://www.google.com/reader/directory/search?q=$feedurl&ck=1199813768546&client=scroll";
$postdata="Email=$username&Passwd=$password&GA3T=5AS_gBsvDHI&nui=15&".
"fpui=3&service=reader&ifr=true&askapache=lovesgoogle&rm=hide&itmpl=true&hl=en&alwf=true&continue=".
$url."&null=Sign in";


$ch = curl_init("https://www.google.com/accounts/ServiceLoginBoxAuth");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $google_cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $google_cookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$askapache_curl_google_result = curl_exec ($ch);
curl_close($ch);
$s=array('@]*?>.*?@si','@]*?>.*?@si','@]*?>.*?@siU','@@');
$g=preg_replace($s, '', $askapache_curl_google_result);
$g=preg_match('@href="([^"]*?)"@si',$g,$m);


$ch = curl_init($m[1]);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $google_cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $google_cookie);
$askapache_curl_google_result = curl_exec ($ch);
curl_close($ch);
$g=preg_match('@href="([^"]*?)"@si',$askapache_curl_google_result,$j);


$ch = curl_init("http://www.google.com/reader/directory/search?q=$feedurl");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $google_cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $google_cookie);
$askapache_curl_google_result = curl_exec ($ch);
curl_close($ch);
$y=preg_match('@class="feed-result-stats">([^<]*?)@si',$askapache_curl_google_result,$s);

// output results
header("Content-type: text/plain");
echo "(".$s[1].") Subscribers for feed: ".urldecode($feedurl);

// delete cookie file
unlink($google_cookie);
exit 0;
exit;
/*
"Google AdSense Automatic Login with PHP and CURL"

http://www.askapache.com/webmaster/login-to-google-adsense-using-php.html

"Follow your Adsense earnings with an RSS reader" 

http://curl.askapache.com/libcurl/php/examples/rss-adsense.html

"Auto-Login to Google Analytics to impress Clients"

http://www.askapache.com/webmaster/login-google-analytics.html

*/
?>

Anyone who runs a dedicated server for web hosting will tell you that a great way to decrease the load on your server and decrease the page load time is to use a PHP Cache such as APC or eAccelerator. While the largest noticeable improvements are for those site that receive a lot of traffic or are under heavy load, any site, large or small can see benefit from a PHP cache. That said, in addition to the two caches mentioned above, a new player has recently entered the market: XCache.

I first started using APC about 2 years ago when the load on one of my servers was high enough that it was affecting load times and was costing me user traffic. I chose APC over eAccelerator because it was a bit easier to install (at the time) and because APC had a reputation for being a bit faster than eAccelerator. Shortly there after I noticed my httpd processes segfaulting and a bit of research also showed that APC had a bit of a record for instability under heavy load. With that in mind, I took the slight performance hit and installed eAccelerator (which is still way faster than using nothing at all).

Up until today, I was still using eAccelerator on all of my servers. However, a post on the vBulletin.com forums prompted me to give XCache, the new PHP accelerator from the maker of lighttpd, a try. I’ve got to say, while I’ve only been using it for about 6 hours at this point, it blows eAccelerator out of the water, especially once you enable multiple caches (which benefits SMP systems).

 

If you’re interested in some benchmarks of XCache, eAccelerator, APC, etc. then checkout the Five Opcode Cache Comparison on PHP on Fire.

 

Read more

To change the configuration for php running as cgi those handy module commands won't work.. The work-around is being able to tell php to start with a custom php.ini file.. configured the way you want.

With multiple custom php.ini files

 -------------------------------------------
 /site/ini/1/php.ini
 /site/ini/2/php.ini
 /site/ini/3/php.ini
 --
 


The trick is creating a wrapper script to set the location of the php.ini file that php will use. Then it exec's the php cgi.

 shell script /cgi-bin/phpini.cgi
 -------------------------------------------
 #!/bin/sh
 export PHPRC=/site/ini/1
 exec /cgi-bin/php5.cgi
 --


Now all you have to do is setup Apache to run php files through the wrapper script instead of just executing the php cgi.

 In your .htaccess or httpd.conf file
 -------------------------------------------
 AddHandler php-cgi .php
 Action php-cgi /cgi-bin/phpini.cgi
 --


So to change the configuration of php you just need to change the PHPRC variable to point to a different directory containing your customized php.ini.. You could also create multiple shell wrapper scripts and create multiple Handler's+Actions in .htaccess..

 in your .htaccess
 -------------------------------------------
 AddHandler php-cgi1 .php1
 Action php-cgi1 /cgi-bin/phpini-1.cgi
 
 AddHandler php-cgi2 .php2
 Action php-cgi2 /cgi-bin/phpini-2.cgi
 
 AddHandler php-cgi3 .php3
 Action php-cgi3 /cgi-bin/phpini-3.cgi
 --


The only caveat here is that it seems like you would have to rename the file extensions, but there are ways around that too outlined by AskApache:  Custom PHP.INI with .htaccess tricks.


Follow

Get every new post delivered to your Inbox.