Category Archives: php
OAuth2 with PHP’s built in Streams Functions
(this drives me nuts, I love upgrading systems but the downside is you have to work with the old ones first and none of the tools you want have been invented yet!)
For OAuth2, all I had to be able to do was to send an Authorization
header with my web request from PHP. My second-favourite way of making API calls from PHP is to use PHP’s stream handling, so I did that. It’s not code you see very often but it’s super-simple and it works on every PHP platform I’ve tried so far, so here’s an example:
// assemble the options $opts = array( 'http'=>array( 'header'=> "Authorization: Bearer " . $access_token ) ); // create the context $context = stream_context_create($opts); // now make the request! Use the context and simply output the result echo file_get_contents('http://api.example.com/endpoint1', false, $context);
If you’re trying to make an API call from PHP and installing better tools is hard for any reason, this example may help!
Relying on A Dev-Master Dependency in Composer
If your project installation instructions recommend requiring dev-master in composer, I may need to reconsider my choice of package
— Lorna Mitchell (@lornajane) December 2, 2015
I got a few responses asking me to expand so I thought I would take the opportunity to write more than 140 characters on this topic. Continue reading
Insert Data with Phinx
change()
description.
One thing I didn’t immediately find was how to insert data. Continue reading
Generating a File List for Phan
phan -f filelist.txt
I generated my filelist.txt
files with a little help from grep – by looking for all files with opening PHP tags in, and putting that list of filenames into a file. My command looks like this:
grep -R -l "<?php" * > filelist.txt
This simply greps recursively (the -R
switch) in all files looking for <?php
and when it finds it, outputs only the filename (the -l
switch does that bit). Then I just put all the output into my filelist.txt
file.
Phan is in its early stages but it’s ready for you to run on your own projects. Look out that you may need to put your bootstrap or other include files first in the filelist.txt
file if phan isn’t finding things in the right order – luckily with it all in one file, it’s relatively easy to move things around if you need to.
PHP: Calling Methods on Non-Objects
PHP Learning Path from O’Reilly
I think it’s a pretty well-rounded collection and it’s only $99 for a couple of weeks, so get the PHP Learning Path here and let me know what you think?
New in PHP 7: null coalesce operator
In PHP 5, we already have a ternary operator, which tests a value, and then returns the second element if that returns true and the third if it doesn’t:
echo $count ? $count : 10; // outputs 10
Continue reading
Joind.in at the PHPNW Hackathon
Joind.in is one of the featured projects and I’m one of the maintainers, so I’ll be at the hackathon and I’m hoping that we’ll get quite a few things done during the evening. Joind.in is an ideal project for events like this since it’s easy to get started with it, and we have a development platform virtual machine (that we’ll have already downloaded onto USB sticks so no conference wifi delay) so you can be up and running in no time. We also have a specific label on our bug tracker for items that we think are manageable for people who don’t already know the system, so chances are that if you want to, you’ll be able to contribute to an open source project with something finished by the end of the night. Continue reading