Category Archives: php
My Book is Published: PHP Master
Continue reading
Announcing Training Dates for November
ArrayAccess vs ArrayObject
Continue reading
At the Helm of Joind.in
Learning About Web Services
Continue reading
PHP OAuth Provider: Access Tokens
Continue reading
PHP Static Analysis Tool Usage
My interest was mostly because I’m working on a book chapter which includes some static analysis content, and there are a couple of these tools that I include in my own builds, but I don’t do much with the output of them. However I didn’t want to drop anything from the chapter if it was actually a valuable tool and I was just missing the point – pretty much all the tools got a good number of votes though, so I’ll be covering all of the above. It does look as if phploc has less of a following, however it’s one of my favourite tools so it gets a mention anyway!
Thanks to everyone who took the time to vote; I thought I’d share the results in case anyone was interested.
Shortening URLs from PHP with Bit.ly
Here’s a simple example, using PHP’s curl extension, of using the bit.ly API to get a short URL, using PHP (you need an API key, but if you’re a registered bit.ly user, you can log in and then find yours at http://bitly.com/a/your_api_key).
$ch = curl_init('http://api.bitly.com/v3/shorten?login=username&apiKey=R_secret&longUrl=http%3A%2F%2Flornajane.net');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r(json_decode($result));
PHP Returning Numeric Values in JSON
It’s just a standard problem of PHP REST services. When I try to access it with java I have to convert it over and over again to ints.
I did have a quick look at the PHP manual page for json_encode but I didn’t see anything mentioning this. A few weeks later (my inbox is a black hole and it takes a while to process these things) I fell over a throwaway comment to an undocumented constant JSON_NUMERIC_CHECK, and I added the constant name to my todo list. In the time it took for me to actually get around to googling for this, some wonderful person updated the PHP manual page (this is why I love PHP) to include it as a documented option, and someone else had added a user contributed note about using it.
It turns out, this constant does exactly what I need. Here’s a simple use case:
echo json_encode(array('event_id' => '603'));
echo json_encode(array('event_id' => '603'), JSON_NUMERIC_CHECK);
and the output:
{"event_id":"603"}
{"event_id":603}
There are probably some situations in which you don’t want all your looks-like-a-number data to be returned as a number, but for now it seems to be a good fit for api.joind.in.