Last active
April 30, 2019 09:39
-
-
Save splittingred/4689218 to your computer and use it in GitHub Desktop.
Example of modRest, a REST Client, in MODX 2.3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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
Cool, I use it, thank you for nice gist!
One note though: in
curlOptions
it is better to useCURLOPT_*
settings instead of modX aliases as all of those parameters get passed to CURL. It leads to silently failingcurl_setopt_array
(as it doesn't recognize the optiontimeout
) and the whole CURL request fails with indistinct error messageNo URL set!
.