Skip to content

Commit

Permalink
Added df_post() method for convenience to make post requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
shannah committed Mar 14, 2018
1 parent 49b6e73 commit 65e2cf1
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion public-api.php
Expand Up @@ -908,5 +908,26 @@ function df_write_json($data){
Dataface_Application::getInstance()->_conf['oe'].'"');
echo json_encode($data);
}


function df_post($url, $data=array(), $json=true) {

// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
throw new Exception("HTTP request failed");
}
if ($json) {
return json_decode($result, true);
}
return $result;
}

} // end if ( !defined( DATAFACE_PUBLIC_API_LOADED ) ){

0 comments on commit 65e2cf1

Please sign in to comment.