我尝试使用ZohoMail的API通过我的应用程序发送电子邮件。但它一直在给我:
" {的errorCode":" INVALID_METHOD"}"状态" {"代码":404,&#34 ;说明":"输入无效"}}
以下是我尝试制作的来电链接:https://www.zoho.com/mail/help/api/post-send-an-email.html#Request_Body
这是我的功能:
public static function sendEmail ($AccountId, $AuthCode, $FromAddress, $ToAddress, $Subject, $Content){
$client = new Client(); //GuzzleHttp\Client
$URI = 'http://mail.zoho.com/api/accounts/' . $AccountId . '/messages';
$headers = ['Content-Type' => 'application/json', 'Authorization' => 'Zoho-authtoken ' . $AuthCode];
$body = array('fromAddress' => $FromAddress, 'toAddress' => $ToAddress, 'subject' => $Subject, 'content' => $Content);
$Nbody = json_encode($body);
$response = $client->post($URI, $headers, $Nbody);
echo "DONE!";
}
我尝试改变拨打电话的方式,但似乎并不是问题所在。我已经在PostMan中测试了这个电话,它运行正常,所以我打电话的方式可能有问题。任何帮助将不胜感激。
答案 0 :(得分:2)
您需要在同一个数组中创建数据和标头,并作为第二个参数传递。像这样使用。
$client = new Client();
$URI = 'http://mail.zoho.com/api/accounts/'.$AccountId.'/messages';
$params['headers'] = ['Content-Type' => 'application/json', 'Authorization' => 'Zoho-authtoken ' . $AuthCode];
$params['form_params'] = array('fromAddress' => $FromAddress, 'toAddress' => $ToAddress, 'subject' => $Subject, 'content' => $Content);
$response = $client->post($URI, $params);
echo "DONE!";
祝你好运!
答案 1 :(得分:1)
$client = new \GuzzleHttp\Client();
$response = $client->post(
'url',
[
GuzzleHttp\RequestOptions::JSON =>
['key' => 'value']
],
['Content-Type' => 'application/json']
);
$responseJSON = json_decode($response->getBody(), true);
答案 2 :(得分:0)
使用Ty:
$response = $client->post($URI, $headers, ['json' => $body]);
而不是
$Nbody = json_encode($body);
$response = $client->post($URI, $headers, $Nbody);
答案 3 :(得分:0)
在使用cURL进行测试后,我发现该网址已被移动'到https而不是http。仅使用http,调用是在Postman中进行的,但不是和Guzzle进行的。我做的唯一改变是建立URL:
<强> https://mail.zoho.com/api/accounts/ 强>
网站将其列为http,并且请求通过PostMan进行。我之前通过相同的API在Guzzle中使用http进行过调用,然后他们就完成了。如果有人可以帮助我理解为什么会发生这种情况,以及为何使用http在PostMan中而不是在Guzzle中进行此特定调用,那就太棒了。
答案 4 :(得分:0)
$this->clients = new Client(['base_uri' => 'Url', 'timeout' => 2.0]);
$params['headers'] = ['Content-Type' => 'application/json'];
$params['json'] = array(
'parama1'=>$req->parama1,
'parama1'=>$req->parama2,
'parama3'=>$req->parama3,
);
$response = $this->clients->get('SearchBiz',$params);
$business = $response->getBody();
return View("myviewbiz")->with('business',json_decode($business));
答案 5 :(得分:0)
这适用于任何地方
use GuzzleHttp\Client;
$client = new Client();
$options = [];
$options['form_params'] = $data;
$options['http_errors'] = false; // for get exception y api response
$options['timeout'] = 5; // milliseconds
$client->request('PUT', $uri , $options);