I have a little laravel app and I added the http://phphttpclient.com/ - httpful REST Client to work with an external api - http://en.help.mailstore.com/spe/Management_API_-_Using_the_API
I was able to "speak" with the mailstore api. Here is one example:
public function GetEnvironmentInfo()
{
$uri = 'https://bla.com:8474/api/invoke/GetEnvironmentInfo';
$response = Httpful::post($uri)
->authenticateWith('admin', 'password')
->send();
$errors = $response->body->error;
$version = $response->body->result->version;
$license = $response->body->result->licenseeName;
dd($version . $license . $errors);
}
That function works. But I have a problem when I try to send additional data to the api.
In the manual of the api you can find the following sentences.
"When a function needs additional data, this data must be send urlencoded. The HTTP header Content-Type: application/x-www-form-urlencoded should be set."
For example I want to set the user password with the help of the api.
SetUserPassword
Set password of MailStore user.
Arguments Name Type Description instanceID string Unique ID of MailStore instance in which this command is invoked. userName string User name of MailStore user. password string Password of MailStore user.
I tried this code.
public function SetUserPassword()
{
$uri = 'bla.com:8474/api/invoke/SetUserPassword';
$response = Httpful::post($uri)
->authenticateWith('admin', 'password')
->addHeaders(array(
'instanceID' => 'bla',
'userName' => 'myuser',
'password' => 'mypassword'
))
->sendsType(\Httpful\Mime::FORM)
->send();
dd($response->body->error);
}
And here is the error response i get:
object(stdClass)[142]
public 'message' => string 'Argument instanceID is missing.' (length=31)
public 'details' => string 'System.Exception: Argument instanceID is missing.
bei #Y3c.#VGc.#X3c(#ncb #tPc, #8xe #I9f, String #GPf, NameValueCollection #P5b, Int32 #joc)
bei #Y3c.#VGc.#Slc(#LLc #Rmc, #8xe #I9f)
' (length=192)
I added the instanceID and the other arguments to the header, but the api can't find the arguments or values.
I think the problem is the urlencode? Can you help me?
Thanks!
In the addHeaders method you need to pass the array ['Content-Type' => 'application/x-www-form-urlencoded']. The rest of the parameters should be passed as the body of the post request.