在 Laravel 中使用 Guzzle 发送 POST 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46652851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending POST request with Guzzle in Laravel
提问by Siddiqui
I'm trying to use ZohoMail's API to send email through my application. But it keeps giving me:
我正在尝试使用 ZohoMail 的 API 通过我的应用程序发送电子邮件。但它不断给我:
"{errorCode":"INVALID_METHOD"},"status":{"code":404,"description":"Invalid Input"}}
"{errorCode":"INVALID_METHOD"},"status":{"code":404,"description":"无效输入"}}
Here's the link to the Call that I'm trying to make: https://www.zoho.com/mail/help/api/post-send-an-email.html#Request_Body
这是我尝试拨打的电话的链接:https: //www.zoho.com/mail/help/api/post-send-an-email.html#Request_Body
Here's my function:
这是我的功能:
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!";
}
I've tried changing the way I'm making the call but it doesn't seem like that's the problem. I've tested the call in PostMan and it works fine so there is probably something wrong with the way I'm making the call. Any help would be much appreciated.
我试过改变我打电话的方式,但似乎这不是问题所在。我已经在 PostMan 中测试了通话,它工作正常,所以我拨打电话的方式可能有问题。任何帮助将非常感激。
采纳答案by Siddiqui
After testing with cURL, I found that the URL had been 'moved' to https instead of http. Using just http, the call was going through in Postman but not with Guzzle. The only change I made was to make the URL:
使用 cURL 进行测试后,我发现 URL 已“移动”到 https 而不是 http。仅使用 http,该呼叫在 Postman 中通过,但在 Guzzle 中未通过。我所做的唯一更改是使 URL:
https://mail.zoho.com/api/accounts/
https://mail.zoho.com/api/accounts/
The website lists it as just http and the request does go through with PostMan. I have made prior calls with just http in Guzzle from the same API and they went through. If someone could help me understand why this happened and why this specific call when using http works in PostMan and not in Guzzle, that'd be great.
该网站将其列为只是 http 并且请求确实通过 PostMan 完成。我之前使用 Guzzle 中的 http 从同一个 API 进行了调用,并且它们都通过了。如果有人能帮助我理解为什么会发生这种情况,以及为什么在 PostMan 而不是 Guzzle 中使用 http 时这个特定调用有效,那就太好了。
回答by Kavan Pancholi
You need to create data and headers in the same array and pass as a second argument. Use like this.
您需要在同一个数组中创建数据和标题并作为第二个参数传递。像这样使用。
$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!";
Good Luck!
祝你好运!
回答by shalonteoh
$client = new \GuzzleHttp\Client();
$response = $client->post(
'url',
[
GuzzleHttp\RequestOptions::JSON =>
['key' => 'value']
],
['Content-Type' => 'application/json']
);
$responseJSON = json_decode($response->getBody(), true);
回答by Niclausel
$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));
回答by Marcin Nabia?ek
Ty to use:
使用:
$response = $client->post($URI, $headers, ['json' => $body]);
instead of
代替
$Nbody = json_encode($body);
$response = $client->post($URI, $headers, $Nbody);