• PHP
  • Ruby on Rails
  • MySQL
  • Linux
    • SELINUX
    • Fedora
    • debian
  • Apache
  • nginx
  • AJAX
Albertech.net

Tag Archives: Php

PHP – Removing alpha characters from an entire array using closure

May 7, 2013 12:40 pm / Albertech.net

Here’s a quick way to remove all alpha characters from an array using PHP closures (PHP 5.3+)

array_walk($myarray, function(&$element){ $element = trim($element,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); });

You can also remove all numbers from an array similarly

array_walk($myarray, function(&$element){ $element = trim($element,"0123456789"); });

The variable must be passed by reference in order to change the values of the original array. I really like the addition of closures, which reduces the need to create a new method for simple operations.

Share this:

  • Facebook
  • Google
  • Twitter
  • Print
  • Email
Posted in: PHP / Tagged: closure, PHP

Installing PHP 5.3 and PHP 5.4 on Mac OSX with HomeBrew

June 6, 2012 9:54 am / Albertech.net

Installing PHP 5.3 and 5.4 is really easy with HomeBrew. PHP is included with Snow Leopard and Lion, but isn’t the latest version. If you want to run a newer version of PHP, such as PHP 5.4, you can install it through HomeBrew.

Here’s a site with good instructions on installing PHP via HomeBrew.
http://justinhileman.info/article/reinstalling-php-53-on-mac-os-x/

The only change that I had to make was with the brew install command since there are now two versions supported (5.3 and 5.4).
PHP 5.3:
brew install php53 --with-mysql --with-intl

PHP 5.4:
brew install php54 --with-mysql --with-intl

Share this:

  • Facebook
  • Google
  • Twitter
  • Print
  • Email
Posted in: Mac OS X, PHP / Tagged: OSX, PHP, reinstall, upgrade

Guide: Using PHP cURL for https requests and a CA certificate

October 5, 2011 6:30 pm / Albertech.net

I’ve looked at a number of cURL PHP tutorials on the web and noticed “curl_setopt($RESTsession, CURLOPT_SSL_VERIFYPEER, false);” is often used for accessing secure websites via cURL. This is often seen when people ask “I cannot connect to HTTPS site using cURL” or  have the “SSL certificate problem, verify that the CA cert is OK” error with cURL.

Ideally, you should set the SSL_VERIFYPEER value to true unless the server you are connecting to does not have a signed certificate. If you are sending confidential data, wouldn’t you want to make sure you are connecting to the correct server?

The first thing you need to do to get https requests is to get the correct CA certificate.

Here’s how to get the CA certificate using Mozilla Firefox 6.

1) On the address bar, visit the secure server that you will need to connect via https:. This will be used to retrieve the certificate file. In this example, I’m using Google.com. Click on the button to the left of the address bar. Click on the “More Information” button.

 

2) A page info window will popup. Click on the “View Certificate” button.

 

3) Click on the “View PEM” button. The “Export” feature does not include the necessary data for cURL to work properly.

 

4) Save the PEM File

 

5) Rename the file to YOURSERVER.pem. Save this file on your web server.

 

Now that you have the certificate, you will need to write the cURL PHP code:

// Replace with your remote server URL
$request = 'https://mysite.com/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request);

// Replace mysite.pem with name of your certificate file
curl_setopt($curl,CURLOPT_CAINFO,getcwd() . "/mysite.pem");

curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);

// Error checking
if(empty($curl)) { echo 'Curl error: ' . curl_error($curl); }
curl_close($curl);

 

 

Modify the $request variable to match the URL of your remote site (e.g. API)  and change the mysite.pem file to the path to your CA file.

If everything works ok, the output of the remote site will be in the $response variable.

Share this:

  • Facebook
  • Google
  • Twitter
  • Print
  • Email
Posted in: PHP / Tagged: certificate, cURL, PHP, ssl

Fix: php_network_getaddresses: getaddrinfo failed: Name or service not known

May 31, 2011 1:18 pm / Albertech.net

I ran across this error today after noticing a file_get_contents was not working. This was on a page that had been working fine for about a year.
“php_network_getaddresses: getaddrinfo failed: Name or service not known”

This issue is typically caused by the Apache/PHP host unable to contact the DNS server.

The first thing to check is to see if you can ping the remote host using console.

  • If no, then your primary name server on /etc/resolv.conf (Debian) is not working. Find a working DNS and restart Apache.
  • If yes, then Apache is still connecting to the broken DNS server. You should try restarting Apache to see if it refreshes the DNS server listing in /etc/resolv.conf.

Other suggestions to fixing this are:
1) Making sure there is an entry inside /etc/hosts for your localhost
2) Firewall rules
3) Manually entering the IP (last resort) You shouldn’t enter the IP since it may change, especially if its a host that you are not in control over.

Share this:

  • Facebook
  • Google
  • Twitter
  • Print
  • Email
Posted in: Apache, PHP / Tagged: getaddrinfo, PHP

PHP: Display page render time

June 12, 2009 2:06 pm / Albertech.net

Here’s what I currently use to determine how long it takes to generate a page in PHP. Its only 4 lines of code.

PHP 5

$time_start = microtime(true);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Displaying the render time: $time seconds\n";

PHP 4 Version: (8 lines)

Put this at the top of the PHP script:

$render_time_start = microtime_float();

Put this at the end of the script:

$render_time_end = microtime_float();
$render_time = $render_time_end - $render_time_start;
echo $render_time;

function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}

Share this:

  • Facebook
  • Google
  • Twitter
  • Print
  • Email
Posted in: PHP / Tagged: PHP, render time, script

Post Navigation

← Older Posts
 

Categories

  • AJAX
  • Android
  • Apache
  • Canon Cameras
  • Cloud
  • CMS
  • Computer Mods
  • Conferences
  • Deals
  • debian
  • Fedora
  • Flash
  • Frameworks
  • git
  • Hardware
  • HTML
  • IDE
  • iPhone
  • iPhone App Review
  • jQuery
  • Linux
  • Mac OS X
  • MySQL
  • nginx
  • PHP
  • portfolio
  • Puppet
  • Ruby on Rails
  • Script Reviews
  • SELINUX
  • Software
  • Software Review
  • SQL Server
  • statistics
  • Tech
  • Tomcat
  • Uncategorized
  • VMWARE
  • VPS
  • Windows
  • wordpress
  • Zend Framework

Blogroll

  • DragonAl Flickr
  • Dropbox – Free 2GB Account
  • James' Blog
  • Javascript Compressor
  • PHP Builder Community
  • PHP-Princess.net
  • Rubular – Regular Expression Validator
  • The Scale-Out Blog
  • Tiny MCE

Tags

activation AJAX android antec Apache AWS awstats canon coda codeigniter debian enclosure external free G1 install vmware tools Internet Explorer iphone 5 jquery Linux mx-1 MySQL office 2007 OSX photoshop PHP plugin plugins portfolio redesigned website review rewrite script security SELinux ssh tinymce tutorial upgrade VMWARE vmware server wordpress wordpress mu XSS zend framework
© Copyright 2013 Albertech.net
Infinity Theme by DesignCoral / WordPress
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.