Archive

Archive for the ‘PHP Solutions’ Category

Twitter Error 417 – Expectation Failed: PHP Solution

June 28th, 2011 No comments

Recently I had to deal with Twitter avatar upload for a Twitter application. Everything went very well, but when I have prepared everything for the upload query, the only server response I got was Error 417.

Since I used cURL, I tried to deal with different headers, but with no luck at all. I found the solution at MSDN website. The correct string is:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Expect:’));

You need to add it to your cURL block. This issue is related to Twitter only; I have no idea why they decided to accept only blank Expect header. Hope it saves you some time.

PHP: strtolower for Russian and Cyrillic

January 18th, 2011 No comments

If you need to work with Russian text in PHP, you may notice that functions that are related to CaSe are not working as expected. I mean strtolower, strtouper, ucwords, and so on. In order to get what you need I suggest to use mb_convert functions. Here is the example:

$string = "Строка";
$string = mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
echo $string;

//output is: строка
?>

You can check reference table for functions, I think you will find more useful functions for you.

Categories: PHP Solutions Tags:

PHP: strpos that takes array as an argument

December 20th, 2010 No comments

Just found one useful function and would kike to share it. If you often use strpos, you might find this useful.

// strpos that takes an array of values to match against a string
// note the stupid argument order (to match strpos)
function strpos_arr($haystack, $needle) {
if(!is_array($needle)) $needle = array($needle);
foreach($needle as $what) {
if(($pos = strpos($haystack, $what))!==false) return $pos;
}
return false;
}
?>

This function will be useful if you need to find whether ANY element of array is presented in the source string. Else you have to either modify it, or create another one.

Categories: PHP Solutions Tags:

How to Prepend a PHP File For a Single Domain

November 26th, 2010 No comments

Sometimes you need to prepend a PHP file, before execution of any file. This can be: website header, or variable declaration, or anything else. In order to do it, we need access to out Virtualhost (which is usually located ad your httpd.conf). If you are using Directadmin, you may find it at /usr/local/directadmin/data/users/. Of course, you need to have root access to modify these values.

Here is our source virtualhost secrion:

ServerName www.lampdocs.com
ServerAlias www.lampdocs.com lampdocs.com
ServerAdmin webmaster@lampdocs.com
DocumentRoot /home/lampdocs/domains/lampdocs.com/public_html
ScriptAlias /cgi-bin/ /home/lampdocs/domains/lampdocs.com/public_html/cgi-bin/
UseCanonicalName OFF
SuexecUserGroup lampdocs lampdocs
CustomLog /var/log/httpd/domains/lampdocs.com.bytes bytes
CustomLog /var/log/httpd/domains/lampdocs.com.log combined
ErrorLog /var/log/httpd/domains/lampdocs.com.error.log


Options +Includes -Indexes
php_admin_flag engine ON

php_admin_flag safe_mode OFF

php_admin_value sendmail_path ‘/usr/sbin/sendmail -t -i -f lampdocs@lampdocs.com’

We will add a string:

php_admin_value auto_prepend_file /var/www/html/docroot.php.

Here is the result:


ServerName www.lampdocs.com
ServerAlias www.lampdocs.com lampdocs.com
ServerAdmin webmaster@lampdocs.com
DocumentRoot /home/lampdocs/domains/lampdocs.com/public_html
ScriptAlias /cgi-bin/ /home/lampdocs/domains/lampdocs.com/public_html/cgi-bin/
php_admin_value auto_prepend_file /var/www/html/docroot.php
UseCanonicalName OFF
SuexecUserGroup lampdocs lampdocs
CustomLog /var/log/httpd/domains/lampdocs.com.bytes bytes
CustomLog /var/log/httpd/domains/lampdocs.com.log combined
ErrorLog /var/log/httpd/domains/lampdocs.com.error.log


Options +Includes -Indexes
php_admin_flag engine ON

php_admin_flag safe_mode OFF

php_admin_value sendmail_path ‘/usr/sbin/sendmail -t -i -f lampdocs@lampdocs.com’

Now reboot Apache and have fun. You can place anything you need in this file. This method does not involve php.ini and allows to prepend a PHP file for a single domain.

Categories: PHP Configuration, PHP Solutions Tags: