POSTrequests from PHP ... and I'd do it completely differently today. So, in an attempt to overcome some of the past crimes of the Internet in general and PHP in particular: here's how to make a POST request in PHP, today, in a PHP 7+ world (it probably works in PHP 5 too). Continue reading
$app variable is the Slim\Slim instance for your application, once you have that, you can just add on any headers you need to with this call to
header(). It wasn't obvious to me and there weren't a lot of resources for this, so I thought I'd share!
file_get_contentsin the general direction of the right URL. Continue reading
In fact, you've been able to pass this as an array since PHP 5.2.10, so to set multiple headers in the stream context, I just used this:
<?php $options = ["http" => [ "method" => "POST", "header" => ["Authorization: token " . $access_token, "Content-Type: application/json"], "content" => $data ]]; $context = stream_context_create($options);
$access_token had been set elsewhere (in fact I usually put credentials in a separate file and exclude it from source control in an effort not to spread my access credentials further than I mean to!), and
$data is already encoded as JSON. For completeness, you can make the POST request like this:
curl http://api.joind.in | python -mjson.tool
You need python installed, but the JSON extension is probably included, and that's all you need for this tool. The result is something like:
You can also use this approach to present JSON data that has been captured to another file, for example, it's a handy trick that I use often when developing something with JSON as a data format.
I have lots of advice for Olly (whom I know personally) but there's no way it will fit into a tweet! So here it is, in rather longer form :)
Once I have the access token, I add the Authorization header using ModHeader and it sends it on all requests to this API, so I can still use my HTML output handler and be logged in. It's useful for sending custom headers of all kinds for different tools, so I thought I'd mention it!
In the first part of this (probably) 3-part series, we'll begin with the basics. It might seem boring, but the most important thing to get right with REST is parsing all the various elements of the HTTP request and responding accordingly. I've put in code samples from from a small-scale toy project I created to make me think about the steps involved (should I put the code somewhere so you can see it? Let me know). Without further ado, let's dive in and begin by sending all requests through one bootstrap script: Continue reading