Skip to content

Instantly share code, notes, and snippets.

@vrudikov
Forked from splittingred/gist:4689218
Created March 12, 2016 10:08
Show Gist options
  • Save vrudikov/cbf2f5642886f006225c to your computer and use it in GitHub Desktop.
Save vrudikov/cbf2f5642886f006225c to your computer and use it in GitHub Desktop.
Example of modRest, a REST Client, in MODX 2.3.
$config = array(
'baseUrl' => rtrim('http://mywebsite.com/rest/api/','/'),
'format' => 'json', // json or xml, the format to request
'suppressSuffix' => false, // if false, will append .json or .xml to the URI requested
'username' => 'myuser', // if set, will use cURL auth to authenticate user
'password' => 'mypass',
'curlOptions' => array(
'timeout' => 30, // cURL timeout
'otherCurlOption' => 1,
),
'headers' => array(
// any HTTP headers to be sent
),
'userAgent' => 'MODX RestClient/1.0.0',
'defaultParameters' => array(
'api_key' => 'if i want an api key passed always for every request',
'other_parameter' => 'these are always sent',
),
);
$client = new modRest($modx,$config);
// POST
$result = $client->post('people',array('name' => 'Joe'));
$response = $result->process();
$id = $response['id'];
// PUT
$client->put('people/'.$id,array('name' => 'Joe','email' => 'joe@doe.com'));
$client->get('people',array('id' => $id));
$client->delete('people/'.$id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment