Klik hier voor werkend PHP-voorbeeld in deze post helemaal onderaan.
Há, gelukkig ben ik voor geen gat te vangen.. Simpele reverse proxy van Apache fixt 't geheel en levert wat leuke HTTP-dingetjes op:
Request via PHP uit dit topic:
POST /api/1.0/tasks HTTP/1.1
Authorization: Basic <KNIP>
Host: asana.mijn.domeintje.tld.invalid
Accept: */*
Content-Length: 106
Content-Type: application/x-www-form-urlencoded
{"name":"CURL CLI-testje 3","notes":"Bladiebla enzo","workspace":"766777600970","assignee":"766774347026"}
Response:
HTTP/1.1 400 Bad Request
Date: Tue, 24 Apr 2012 17:13:18 GMT
Server: nginx/0.7.67
Cache-control: no-cache="set-cookie"
Content-Type: application/json
X-Asana-Content-String-Length: 51
X-Asana-Preferred-Release-Revision: 20120424_134406_17c953aae206375f9a10c0c6305633e2a5d8211d
Content-Length: 51
Set-Cookie: server=ws_15|T5bfM; path=/
Set-Cookie: <geen idee waar dit voor is, maar knip 't toch maar weg, vast niet nuttig nu>
Connection: close
{"errors":[{"message":"workspace: Missing input"}]}
Request via `curl` via cmd:
POST /api/1.0/tasks HTTP/1.1
Authorization: Basic <KNIP>
User-Agent: curl/7.24.0 (i686-pc-linux-gnu) libcurl/7.24.0 OpenSSL/1.0.0h zlib/1.2.5
Host: asana.mijn.domeintje.tld.invalid
Accept: */*
Content-Length: 88
Content-Type: application/x-www-form-urlencoded
name=CURL CLI-testje 3¬es=Bladiebla enzo&workspace=766777600970&assignee=766774347026
Response:
HTTP/1.1 201 Created
Date: Tue, 24 Apr 2012 17:13:38 GMT
Server: nginx/0.7.67
Cache-control: no-cache="set-cookie"
Content-Type: application/json
Location: /api/1.0/tasks/766970116549
X-Asana-Content-String-Length: 421
X-Asana-Preferred-Release-Revision: 20120424_134406_17c953aae206375f9a10c0c6305633e2a5d8211d
X-Asana-Release-Revision: 20120424_134406_17c953aae206375f9a10c0c6305633e2a5d8211d
Content-Length: 421
Set-Cookie: server=ws_2|T5bfR; path=/
Set-Cookie: <knip>
{"data":{"id":766970116549,"created_at":"2012-04-24T17:13:38.490Z","modified_at":"2012-04-24T17:13:38.490Z","name":"CURL CLI-testje 3","notes":"Bladiebla enzo","completed":false,"assignee_status":"inbox","completed_at":null,"due_on":null,"projects":[],"assignee":{"id":766774347026,"name":"osirisinferi"},"workspace":{"id":766777600970,"name":"OsirisInferi Ltd."},"followers":[{"id":766774347026,"name":"osirisinferi"}]}}
Overigens, als je PHP lekker z'n werk laat doen zonder JSON, dan doet 'ie 't inderdaad niet, omdat 'ie dan wel heel erg suffe shit doet:
Request zonder JSON via CURL in PHP:
POST /api/1.0/tasks HTTP/1.1
Authorization: Basic <knip>
Host: asana.mijn.domeintje.tld.invalid
Accept: */*
Content-Length: 483
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------4a5e7a242d8b
HTTP/1.1 100 Continue
------------------------------4a5e7a242d8b
Content-Disposition: form-data; name="name"
CURL CLI-testje 3
------------------------------4a5e7a242d8b
Content-Disposition: form-data; name="notes"
Bladiebla enzo
------------------------------4a5e7a242d8b
Content-Disposition: form-data; name="workspace"
766777600970
------------------------------4a5e7a242d8b
Content-Disposition: form-data; name="assignee"
766774347026
------------------------------4a5e7a242d8b--
En ja, da's met "CURLOPT_POST" op true, waarbij volgens php.net dan toch écht "application/x-www-form-urlencoded" gebruikt zou moeten worden
Ah, gevonden waarom. Ligt aan hoe je CURLOPT_POSTFIELDS aanlevert: "If value is an array, the Content-Type header will be set to multipart/form-data."
En bam, gelukt via PHP:
POST /api/1.0/tasks HTTP/1.1
Authorization: Basic <knip>
Host: asana.mijn.domeintje.tld.invalid
Accept: */*
Content-Length: 88
Content-Type: application/x-www-form-urlencoded
name=CURL CLI-testje 3¬es=Bladiebla enzo&workspace=766777600970&assignee=766774347026HTTP/1.1 201 Created
Date: Tue, 24 Apr 2012 17:24:59 GMT
Server: nginx/0.7.67
Cache-control: no-cache="set-cookie"
Content-Type: application/json
Location: /api/1.0/tasks/767036917352
X-Asana-Content-String-Length: 421
X-Asana-Preferred-Release-Revision: 20120424_134406_17c953aae206375f9a10c0c6305633e2a5d8211d
X-Asana-Release-Revision: 20120424_134406_17c953aae206375f9a10c0c6305633e2a5d8211d
Content-Length: 421
Set-Cookie: server=ws_4|T5bh7; path=/
Set-Cookie: <knip>
{"data":{"id":767036917352,"created_at":"2012-04-24T17:24:58.882Z","modified_at":"2012-04-24T17:24:58.882Z","name":"CURL CLI-testje 3","notes":"Bladiebla enzo","completed":false,"assignee_status":"inbox","completed_at":null,"due_on":null,"projects":[],"assignee":{"id":766774347026,"name":"osirisinferi"},"workspace":{"id":766777600970,"name":"OsirisInferi Ltd."},"followers":[{"id":766774347026,"name":"osirisinferi"}]}}
Code:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| <?php
$key = '<knip>';
$data = 'name=CURL CLI-testje 3¬es=Bladiebla enzo&workspace=766777600970&assignee=766774347026';
$url = 'http://asana.mijn.domeintje.tld.invalid/api/1.0/tasks';
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_POST, true); // set POST to true
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // the data to be POST'ed
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $key.':');
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$info = curl_getinfo($ch);
$returndata = curl_exec ($ch);
curl_close($ch);
print_r($returndata); |
[
Voor 23% gewijzigd door
Osiris op 24-04-2012 19:30
]