2011年11月23日星期三

Lorna Mitchell's Blog: POSTing JSON Data With PHP cURL


On her blog today Lorna Mitchell has a quick tip for anyone having an issue sending POSTed JSON data with the curl functionality that can be built into PHP. The trick to her method is sending things with the right header.



We can't send post fields, because we want to send JSON, not pretend to be a form (the merits of an API which accepts POST requests with data in form-format is an interesting debate). Instead, we create the correct JSON data, set that as the body of the POST request, and also set the headers correctly so that the server that receives this request will understand what we sent.


She includes a code example (about ten lines) showing the POSTing process that sets up options using curl's curl_setopt. Be sure to set up the headers to send as "application/json" - that's the trick to letting the remote end know the format.

Community News: Latest Releases from PHPClasses.org

2011年11月18日星期五

Site News: Popular Posts for the Week of 11.18.2011

Popular posts from PHPDeveloper.org for the past week:

DZone.com: Creating a virtual server with Vagrant: a practical walkthrough


On DZone.com there's a new post from Giorgio Sironi looking at how to automate a build of a virtual server with Vagrant, setting up a LAMP-based development instance.



Vagrant ia a tool for building virtual machines (in VirtualBox's format) that conforms to a specification. It's written in Ruby, but it makes really no assumptions over the environments that you're gonna build; in this article, we will setup a virtual server for PHP applications running inside Apache.


The end result is a virtual machine based on VirtualBox images and can be built in a few easy steps:



  • install the vagrant gems on the build system
  • add a new virtual box instance pointed to a .box file
  • create the Vagrant config (including the commands to run post-create)
  • set up a little port forawrding
  • creating a phpinfo file and starting up Apache


One suggested place for grabbing images (some with pre-defined software) is Bitnami's "Stacks" repository.

DevShed: Building an ORM in PHP


On DevShed today there's a new tutorial showing you how to build a basic ORM layer on top of a MySQL database. It includes all the code you'll need (cut&paste-able, not as a download).



Obviously, with so many ORMs at one's disposal for free, it seems pretty pointless to develop a custom one; are we trying to reinvent the wheel? No, of course not. But if you need to create a simple application that performs a few CRUD operations on some related domain objects and don't want to climb the learning curve of a third-party library, then implementing a custom ORM might make sense. There's alos the educational aspect of the process (yes, learning one or two things never hurts).


They start you off with the creation of the "data persistence layer" (an interface first) to connect to the database, building a MySQL-specific one on top of it. Next up is the data mapper layer making things like "fetch by ID" and the insert/update/delete possible. Their simple example doesn't include anything about ORM relationships, though - just fetching simple rows.