2

我有一个小 laravel 应用程序,我添加了http://phphttpclient.com/ - httpful REST 客户端以使用外部 api - http://en.help.mailstore.com/spe/Management_API_-_Using_the_API

我能够用 mailstore api“说话”。这是一个例子:

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);
}

该功能有效。但是当我尝试向 api 发送其他数据时遇到问题。

在 api 的手册中,您可以找到以下句子。

“当一个函数需要额外的数据时,这个数据必须发送urlencoded。应该设置HTTP头Content-Type:application/x-www-form-urlencoded。”

例如我想在api的帮助下设置用户密码。

设置用户密码

设置 MailStore 用户的密码。

参数 名称 类型 描述 instanceID string 调用此命令的 MailStore 实例的唯一 ID。userName string MailStore 用户的用户名。password string MailStore 用户的密码。

我试过这段代码。

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);
}

这是我得到的错误响应:

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)

我将 instanceID 和其他参数添加到标题中,但 api 找不到参数或值。

我认为问题是urlencode?你能帮助我吗?

谢谢!

4

1 回答 1

1

在 addHeaders 方法中,您需要传递数组 ['Content-Type' => 'application/x-www-form-urlencoded']。其余参数应作为 post 请求的主体传递。

于 2014-10-10T09:07:32.900 回答