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.
没有评论:
发表评论