Tag Archives: php

How to use http_build_url with pecl_http version 2.0 with PHP 5.4, 5.5, and 5.6

The old way to use http_build_url This is how to use http_build_url with pecl_http version 1.x: $parse = parse_url(‘http://example.com/index.asp?test=123#hash’); print_r($parse); # # Yields a result of: # # Array # ( # [scheme] => http # [host] => example.com # [path] => /index.asp # [query] => test=123 # [fragment] => hash # ) $url = http_build_url($parse); echo $url; # # Yields a result of: # # http://example.com/index.asp?test=123#hash The new way to use http_build_url with pecl_http version 2.x: This functionality has now moved into the http\Url class: $parse = parse_url(‘http://example.com/index.asp?test=123#hash’); print_r($parse); # # Yields a result of: # # Array # ( # [scheme] => http # [host] => example.com # [path] => /index.asp # [query] => test=123 # [fragment] => hash # ) $url = new \http\Url($parse); echo $url->toString(); # # Yields a result of: # # http://example.com/index.asp?test=123#hash #

Posted in Tech Tips | Tagged , | Leave a comment

How to use http_get_request_headers with PECL_HTTP version 2

If you’re using the version 2 pecl_http (possibly version 2.0.6?) on your webserver, perhaps Ubuntu or CentOS, with PHP5, maybe 5.3, 5.4, or 5.5, you may have noticed that after getting it all installed and adding this to your php.ini file: extension=raphf.so extension=propro.so extension=http.so But then when you try and use the function get_request_headers() you end up getting: PHP Fatal error: Call to undefined function http_get_request_headers() Well, when this extension switched to pecl/http v2 it changed a lot of things, and global functions was one of them. It now uses namespaces, and so instead of using http_get_request_headers() you’ll need to use something like this: $headers = \http\Env::getRequestHeader(); print_r($headers); The check out the docs for more details on how to use the new function, basically you’ll see getRequestHeader: Retrieve one or all headers of the current HTTP request. Parameters: Optional string $header_name The key of a header to retrieve. Returns: NULL, if $header_name was not found string, the compound header when $header_name was found array of all headers if $header_name was not specified

Posted in Web Development | Tagged , | Leave a comment

How to install pecl pecl_http with homebrew php54 / php55

You may be tempted to try and run pecl install pecl_http if you’re using homebrew on Mac OS X in order to be able to access functions like http_get_request_headers – but there’s a better way. Instead, try using the command brew install php54-http or brew install php55-http ( depending on if you are using php 5.4 or php 5.5 with homebrew ) You can get more info about pecl_http with homebrew by using brew info php54-http or brew info php55-http ( again depending on your PHP version ). If you have already tried pecl install pecl_http you may want to run pecl uninstall pecl_http before running the other commands. If you have previously used something like brew install php54-http and now it doesn’t seem to be loading, check your configuration file in /usr/local/etc/php/5.4/conf.d or try doing: brew remove php54-http And then running: brew install php54-http

Posted in Tech Tips | Tagged , | Leave a comment

Hombrew Upgrade PHP54 – make ext/ldap/ldap.lo Error 1

If you are trying to upgrade brew / homebrew’s PHP54 package by josegonzalez – especially if you have oci8.so / pdo_oci.so you might be getting an error like this: The actual text of the message is something like: $ brew upgrade Updated Homebrew from 5b3410b6 to 44c284ff. ==> Upgrading 2 outdated packages, with result: php54 5.4.22, php54-mcrypt 5.4.22 ==> Upgrading php54 ==> Downloading http://www.php.net/get/php-5.4.22.tar.bz2/from/this/mirror Already downloaded: /Library/Caches/Homebrew/php54-5.4.22 Warning: Backing up all known pear.conf and .pearrc files Warning: If you have a pre-existing pear install outside of homebrew-php, or you are using a non-standard pear.conf location, installation may fail. ==> ./configure –prefix=/usr/local/Cellar/php54/5.4.22 –localstatedir=/usr/local/var –sysconfdir=/usr/local/etc/php/5.4 –with-config-file-path=/usr/local/etc/php/5 ==> make rc = ldap_sasl_interactive_bind_s(ld->link, binddn, ctx->mech, NULL, NULL, LDAP_SASL_QUIET, _php_sasl_interact, ctx); ^ 11 errors generated. make: *** [ext/ldap/ldap.lo] Error 1 make: *** Waiting for unfinished jobs…. READ THIS: https://github.com/mxcl/homebrew/wiki/troubleshooting If reporting this issue please do so at (not mxcl/homebrew): https://github.com/josegonzalez/homebrew-php/issues ==> Upgrading php54-mcrypt Warning: Could not link php54. Unlinking… Why does this happen? In my case it was because I installed oracle instaclient and was using that alongside homebrew. It has library files referenced in /usr/local/include and they don’t play nice when you upgrade PHP / php54 with homebrew. How do I fix it? Open terminal and change to /usr/local/include/ Type in something like ls -al | grep ldap to be presented with something like this: lrwxr-xr-x 1 user group 52B Nov 19 21:37 ldap.h -> /usr/local/instantclient/ Copy and paste that – make sure you write down the path of where that symlink is pointing. … Continue reading

Posted in Tech Opinion | Tagged , , , , | 1 Comment

What is ming for PHP (php5-ming) ?

Reading a recent server setup guide I saw the suggestion to use apt-get install php5-ming to install Ming – as a (suggested) module that WordPress would like to have. so what is MING ? First of all: Ming is not an acronym. Ming is an open-source (LGPL) library which allows you to create SWF (“Flash”) format movies. Ming supports almost all of Flash 4’s features, including: shapes, gradients, bitmaps (pngs and jpegs), morphs (“shape tweens”), text, buttons, actions, sprites (“movie clips”), streaming mp3, and color transforms –the only thing that’s missing is sound events. Note that all values specifying length, distance, size, etc. are in “twips”, twenty units per pixel. That’s pretty much arbitrary, though, since the player scales the movie to whatever pixel size is specified in the embed/object tag, or the entire frame if not embedded. More information about ming is available in the PHP documentation

Posted in Web Development | Tagged , | Leave a comment

PHP: Convert Ascii To Hex

When working in PHP there are times when I want to see what a string is in HEX – a lot of times there are invisible characters that or non-printing strings that can throw your program or script for a loop. If you save the string to a text file, you can use a hex editor utility, like hexdump to take a look at the hex codes of the ASCII characters: $ echo abcd > test.txt $ hexdump test.txt 0000000 61 62 63 64 0a 0000005 $ Now for the PHP version: header(‘Content-type: text/plain; charset=utf-8’); $ascii = ‘abcd’; $hex = unpack(‘H*’,$ascii); // you can print out the hex result of the ascii conversion: print_r($hex); # Array # ( # [1] => 61626364 # ) // or for a nicer display you can add spaces between every two characters // and wrap the column every 6 characters (change to fit your needs) echo wordwrap(implode(‘ ‘, str_split($hex[1], 2)), 6); # 61 62 # 63 64

Posted in Tech Tips, Web Development | Tagged , , | Leave a comment

PHP rjust

Python has a neat feature that does right alignment of strings with the syntax: string.rjust(padding,fill_char) The method rjust() returns the string right justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than len(s). Below you can find how to do it in PHP php rjust function // by techblog.willshouse.com function rjust($string,$total_length,$fillchar=’ ‘) { // if the string is longer than the total length allowed just return it if(strlen($string) >= $total_length) { return $string; } $total_length = intval($total_length); // total_length must be a number greater than 0 if( ! $total_length ) { return $string; } // the $fillchar can’t be empty if(!strlen($fillchar)) { return $string; } // make the fill character into padding while(strlen($fillchar) < $total_length) { $fillchar = $fillchar.$fillchar; } return substr($fillchar.$string, ( -1 * $total_length )); } test php rjust function header(‘Content-type: text/plain’); $items = array(‘one’,’a’,’superduper’); foreach($items as $item) { echo rjust($item,15); echo “\n”; } php rjust function output one a superduper

Posted in Tech Tips, Web Development | Tagged | Leave a comment

Uncaught exception ‘CFCredentials_Exception’ with message ‘No credentials were provided. [solved]

If you’re using the Amazon Web Services (AWS) SDK (Software Developer Kit) version 1.5 “Allegro” for PHP you may have noticed an error like: Fatal error: Uncaught exception ‘CFCredentials_Exception’ with message ‘No credentials were provided. The SDK attempts to retrieve Instance Profile credentials from the EC2 Instance Metadata Service, but doing this requires the “default_cache_config” option to be set in the config.inc.php file or constructor. In order to cache the retrieved credentials.’ in #0 amazon-php-sdk/sdk- Stack trace: amazon-php-sdk/sdk- CFRuntime->__construct(Array) #1 amazon-php-sdk/sdk- AmazonEC2->__construct() #2 {main} thrown in amazon-php-sdk/sdk- on line 439 My solution was fairly simple – the PHP SDK download has a config-sample.inc.php file that you can copy to config.inc.php and then add your AWS key and secret key. After doing that you should be good to go.

Posted in Server Administration, Software, Tech Tips, Web Development | Tagged , , , | 2 Comments

First Impressions of Laravel

After having done substantial work with PHP, including using many MVC frameworks such as ExpressionEngine, CodeIgniter, CakePHP, FuelPHP, and Zend I have to say that Laravel is surprisingly refreshing. The creators have put a real emphasis on eloquent code, and code that is high-level / highly readable (as english). I like the static methods and helpers that the framework provides. The MySQL integration is nice, too, with both an easy query building helper, as well as an eloquent ORM. (The only thing I really don’t agree with is renaming “limit” to “take” – it’s not intuitive as it is not related to the underlying technology – leave it as limit please!) The artisan command line tool is also nice to help manage migrations. Laravel also has some pretty good built-in functionality, offering some authentication, validation, and csrf checking. The routes and controllers are also easy to set up for use with a RESTFUL model. Finally, I like that it is focused on the strengths of PHP. With some other frameworks (fuelPHP) I felt that they were powerful but were trying a little to hard to be Ruby / Ruby on Rails. All-in-all I am enjoying using it and I hope that it continues to be developed and gains traction in the PHP community.

Posted in Software, Tech Opinion, Web Development | Tagged , , | Leave a comment

How can I parse a .torrent file with PHP? [solved]

There are some similar questions, but most of them want to download a torrent using PHP or something like that. What I’d like to do is parse a .torrent file, add trackers to it, and then re-save it as a .torrent file. I saw Snark which looks to be for Java only. Can I do this with PHP? If so, how? Solution To parse a .torrent file with PHP you can use a standalone function, as demonstrated here or you can use a library, like the one included with php-bittorrent

Posted in Tech Tips, Web Development | Tagged , , , | Leave a comment