PHP and CURL


function browse($url, $postData = null) {
$fields = '';
if(is_array($postData) && sizeof($postData) >= 1){
$fields = http_build_query($postData);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if($fields !=='') {
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
}

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_COOKIESESSION, true);
// Write cookie if needed .. also try: dirname(__FILE__) . '/cookie.txt';
curl_setopt($ch, CURLOPT_COOKIEJAR , '/tmp/cookie.txt');
// Read cookie if needed
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie.txt');

$result = curl_exec($ch);
curl_close($ch);

return $result;
}

Adding New VirtualHost in Apache2

On linux Ubuntu
1. If your apache has /etc/apache2/sites-available directory then just create a new file “dev.newwebsite.com.conf”
with the following content:


ServerName dev.newwebsite.com
DocumentRoot "/var/www/www.newwebsite.com"

# Error handlers
ErrorDocument 500 /errordocs/500.html


AllowOverride All
Options -Indexes FollowSymLinks
Allow from all


2. Also copy the same file into /etc/apache2/sites-enabled

3. Restart your apache

$ /etc/init.d/apach2 restart

4. Edit your hosts file: /etc/hosts
add the following line to it
127.0.0.1 dev.newwebsite.com

5. Open your web browser and open http://dev.newwebsite.com

If you are a windows user and are using xampp then you need to alter the vhost configuration file.
The default xampp file is in
“\xampp\apache\conf\extra\httpd-vhosts”
Then restart your apache

Memcache test connection script


connect('127.0.0.1', 11211) or die ("Could not connect");

// Add it to memcached server
// The parameters are: KEY, VALUE, USE COMPRESSION, EXPIRY IN SECONDS
$memcache->set('MyKey1', 'The value of My Key1 is me', false, 100);

echo $memcache->get('MyKey1');
// It will show you: The value of My Key1 is me
?>

Redis connection test script


"tcp",
"host" => "127.0.0.1",
"port" => 6379));
*/
echo "Connected to Redis";
}
catch (Exception $e) {
echo "Unable to connect to Redis";
echo $e->getMessage();
}

$redis->set("var1", "The value for var1 is me");
echo $redis->get("var1");

// You can alos use exits() function to test if a variable exists or not
echo ($redis->exists("var2")) ? "true" : "false";

Introduction to Logging and Tracing in PHP

If you still tracing variable in php using print_r() or var_dump() then you’ll see them directly in the screen or the web page that you’re working on. This is easy to see, but also prone to problems:

  1. What if visitors see your error trace? Surely you don’t want that.
  2. What if you have a function call that relies on no string has been printed yet, e.g.: header(), ini_set(), these functions will fail and depending on your settings most likely when they fail they don’t show any sign of error.

Best way to do code tracing in PHP is to write the following code into your file – one of the early-executed file, such as config/header file, where you don’t have any output yet:

ini_set('log_errors','On');
ini_set('error_log', '/var/wwwtmp/php.log');
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));

And at the part where you want to trace a certain variable

error_log(__FILE__.' ('.__LINE__.') '.__CLASS__.' :: '. __FUNCTION__ .' :: '. print_r($YOUR_VARIABLE_HERE,true));

By default apache2 and php will output their error in /var/log/apache2/error.log ,but after executing the ini_set above, the error log will be produced in /var/wwwtmp/php.log. And you need to make sure you that directory exists, if not use other directory that www-data has write access to. Why do we need to setup in a different location? Because the default file output doesn’t support showing new lines, you’ll see lot’s of \n. Thus make tracing hard to read.

Check For Loaded PHP Extensions

Sometimes you want to make sure a certain php extension is loaded or not. These extension can be curl, xmlrpc, or ldap. After all when you move your application from one server to another the configuration not necessarily the same. So here how you do check for loaded or existing php extensions using get_loaded_extensions().


$extNeeded = array('ldap', 'mysql', 'xmlrpc','ron_ext');
$loadedExtension = get_loaded_extensions();
foreach($extNeeded as $ext){ 
 if(!in_array($ext, $loadedExtension)){
  echo 'The application cannot start properly, '.$ext.' extension is missing. 
  The following extensions are needed: '.implode(', ', $extNeeded).'. 
  Terminating application ...';
  die;
 }
}