Skip to content

Instantly share code, notes, and snippets.

@duan-li
Created July 5, 2015 23:46
Show Gist options
  • Save duan-li/d5bf99d455ba498b138f to your computer and use it in GitHub Desktop.
Save duan-li/d5bf99d455ba498b138f to your computer and use it in GitHub Desktop.
post json request with
$url = "http://your_api_address.com";
$json = '
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"
,"X-User-Email: someone@email.com"
,"X-Auth-Token: some_token_code"
));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
var_dump($response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment