Appearance
Examples
Rest auth and call an API method
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);
}
Rest auth
// 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);
Rest GET/POST/PUT/DELETE
// 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);
Rest simple call (create_entry)
// auth can also be an shortkey, or username password
$data = array(
'auth' => array(
'key' => '83a4eac91b99ea0b3e3bb62e54d9c1b3',
'secret' => '37e13a465cbd94b8e4cd4dbd260e2a22'
),
'data' => array(
'channel_name' => 'news',
'status' => 'Open',
'title' => 'test entry',
'sticky' => 'y',
'allow_comments' => 'n',
'category' => '1|2|3',
'grid' => array(
'rows' => array(
array(
'cell_1' => 'row 1',
'cell_2' => 'row 11'
),
array(
'cell_1' => 'row 2',
'cell_2' => 'row 22'
),
),
),
'matrix' => array(
'rows' => array(
array(
'cell_1' => 'row 1',
'cell_2' => 'row 11'
),
array(
'cell_1' => 'row 2',
'cell_2' => 'row 22'
),
),
),
)
);
//url to post to
$url = "http://ee346.local/index.php/webservice/rest/create_entry";
$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));
print_r($response);
curl_close ($ch);
Rest simple call (read_entry)
// auth can also be an shortkey, or username password
$data = array(
'auth' => array(
'key' => '83a4eac91b99ea0b3e3bb62e54d9c1b3',
'secret' => '37e13a465cbd94b8e4cd4dbd260e2a22'
),
'data' => array(
'entry_id' => 39
)
);
//url to post to
$url = "http://ee346.local/index.php/webservice/rest/read_entry";
$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));
print_r($response);
// Loop over the entries
foreach($response->data as $entry) {
print_r($entry);
}
curl_close ($ch);
Soap auth and call an API method
function parse_soap_data($data)
{
$new_soap_data = array();
foreach($data['data'] as $key => $row)
{
foreach($row as $value)
{
$new_soap_data[$key][$value['key']] = $value['value'];
}
}
unset($data['data']);
return array_merge($data, array( 'data' => $new_soap_data));
}
//create a soap request
$client = new SoapClient('http://ee346.local/index.php/webservice/soap?wsdl', array('trace' => 1));
//auth via username
$session_data = $client->authenticate(array(
'key' => '83a4eac91b99ea0b3e3bb62e54d9c1b3',
'secret' => '37e13a465cbd94b8e4cd4dbd260e2a22'
));
//parse the data so we get rid of all keys and values in separated arrays
$session_data = parse_soap_data($session_data);
//create a new entry with the session_id as inlog
$reponse = $client->create_entry(array(
'session_id' => $session_data['data'][0]['session_id'],
), array(
'channel_name' => 'news',
'status' => 'Open',
'title' => 'test entry',
'sticky' => 'y',
'allow_comments' => 'n'
));
print_r(parse_soap_data($reponse));
Soap auth
//create a soap request
$client = new SoapClient('ee346.local/index.php/webservice/soap?wsdl', array('trace' => 1));
//auth via username
$session_data = $client->authenticate(array(
'key' => '83a4eac91b99ea0b3e3bb62e54d9c1b3',
'secret' => '37e13a465cbd94b8e4cd4dbd260e2a22'
));
print_r($session_data);
Soap simple (create_entry)
function parse_soap_data($data)
{
$new_soap_data = array();
foreach($data['data'] as $key => $row)
{
foreach($row as $value)
{
$new_soap_data[$key][$value['key']] = $value['value'];
}
}
unset($data['data']);
return array_merge($data, array( 'data' => $new_soap_data));
}
//create a soap request
$client = new SoapClient('http://ee346.local/index.php/webservice/soap?wsdl', array('trace' => 1));
//create a new entry with the session_id as inlog
$reponse = $client->create_entry(array(
'key' => '83a4eac91b99ea0b3e3bb62e54d9c1b3',
'secret' => '37e13a465cbd94b8e4cd4dbd260e2a22'
), array(
'channel_name' => 'news',
'status' => 'Open',
'title' => 'test entry',
'sticky' => 'y',
'allow_comments' => 'n'
));
print_r(parse_soap_data($reponse));
XMLRPC auth and call method
// install XMLRPC lib: https://github.com/gggeek/phpxmlrpc
// via composer use "composer require phpxmlrpc/phpxmlrpc"
$c = new PhpXmlRpc\Client(http://ee346.local/index.php/webservice/xmlrpc);
$x = new PhpXmlRpc\Request('read_entry', array(
PhpXmlRpc\Encoder::encode(array(
'session_id' => ee()->session->userdata('session_id')
)),
PhpXmlRpc\Encoder::encode(Array(
'site_id' => 1,
'entry_id' => 39
)),
)));
$c->return_type = 'phpvals';
$r = $c->send($x);
print_r($r);
XMLRPC auth
// install XMLRPC lib: https://github.com/gggeek/phpxmlrpc
// via composer use "composer require phpxmlrpc/phpxmlrpc"
$c = new PhpXmlRpc\Client(http://ee346.local/index.php/webservice/xmlrpc);
$x = new PhpXmlRpc\Request('authenticate', array(
PhpXmlRpc\Encoder::encode(array(
'key' => '83a4eac91b99ea0b3e3bb62e54d9c1b3',
'secret' => '37e13a465cbd94b8e4cd4dbd260e2a22'
))
)));
$c->return_type = 'phpvals';
$r = $c->send($x);
print_r($r);