All you need to know is on the date documentation…
Tag Archives: php
PHP file_get_contents with HTTP Basic Auth
Today I needed to figure out how to read some data from a URL that required HTTP Basic Auth. The solution was pretty simple, use file_get_contents and pass in a configured stream context. I found the following code on the stream_context_create documentation:
$cred = sprintf( 'Authorization: Basic %s',
base64_encode( 'username:password' )
);
$opts = array(
'http' => array(
'method' => 'GET',
'header' => $cred
)
);
$ctx = stream_context_create( $opts );
$data = file_get_contents( $url, false, $ctx );
Easy-peasy!
PHP urlencode
Used PHP’s urlencode today…
Sorting Arrays in PHP
Everything you ever wanted to know about sorting arrays in PHP.
PHP instance of Closure vs is_callable or function_exists
The problem is that is_callable and function_exists check a string to see if it’s a function. But I wanted to check to see if it was an anonymous function and not a string. Turns out you can do this by checking for an instance of Closure:
if ( $fn instanceof Closure ) { $fn( ... ); }
PHP HTTP response data
Today I learned about headers_list and http_response_code… yay! :)
PHP fflush
spl_object_hash
Learned about spl_object_hash via Stack Overflow. Provides a unique object id in PHP.
PHP Traits and Grafts
Reading about traits and grafts in PHP: Request for Comments: Horizontal Reuse for PHP, PHP Traits, Using Traits in PHP 5.4 and PHP Traits: Good or Bad?. They mentioned the Diamond Problem.
I learned some new syntax too! E.g.:
return $_instance ?: $_instance = new $class;
The ternary operator as a ‘binary’ operator! Cool! See here for details: Since PHP 5.3, it is possible to leave out the middle part of the ternary operator.
PHP range — Create an array containing a range of elements
Today I discovered the PHP range function…