// Data array
$data = array(
'data' => array(
'key' => '83a4eac91b99ea0b3e3bb62e54d9c1b3',
'secret' => '37e13a465cbd94b8e4cd4dbd260e2a22'
)
);
//url to post to
$url = "http://ee346.local/index.php/webservice/rest/authenticate";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1); //0 for a get request
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
curl_setopt($ch,CURLOPT_TIMEOUT, 20);
$response = json_decode(curl_exec($ch));
if($response->success)
{
echo 'member_id is: '.$response->data[0]->member_id;
echo '<br>';
echo 'session_id is: '.$response->data[0]->session_id;
//with the session_id you can auth yourself for all other calls. Just use the auth[session_id]=your-id-id in your next call to a api method
}
curl_close ($ch);
function fetch_data($api_method = '', $data = array())
{
//url to post to
$url = "http://ee346.local/index.php/webservice/rest/".$api_method;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1); //0 for a get request
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
curl_setopt($ch,CURLOPT_TIMEOUT, 20);
$response = curl_exec($ch);
curl_close ($ch);
return json_decode($response);
}
// auth with the webservice
$auth_response = fetch_data('authenticate', $postvars = array(
'data' => array(
'key' => '83a4eac91b99ea0b3e3bb62e54d9c1b3',
'secret' => '37e13a465cbd94b8e4cd4dbd260e2a22'
))
);
//auth succesfully
if($auth_response->success)
{
//with the session_id you can auth yourself for all other calls. Just use the auth[session_id]=your-id-id in your next call to a api method
$response = fetch_data('read_entry', array(
'auth' => array(
'session_id' => $auth_response->data[0]->session_id
),
'data' => array(
'entry_id' => 39
)
));
print_r($response);
}
// auth can also be an shortkey, or username password
$data = array(
'auth' => array(
'key' => '83a4eac91b99ea0b3e3bb62e54d9c1b3',
'secret' => '37e13a465cbd94b8e4cd4dbd260e2a22'
),
'data' => array(
'entry_id' => 39,
'title' => 'new title',
// or any other custom field can be add here
'custom_field' => 'Updated value'
)
);
// url to post to
// to use the GET/POST/PUT/DELETE you need to post to the API name, not the method.
$url = "http://ee346.local/index.php/webservice/rest/entry";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // you can use GET/POST/PUT/DELETE
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = json_decode(curl_exec($ch));
print_r($response);