我正在尝试使用 Guzzle 从我的服务器发出请求,而不是要求。我使用 cURL 并且这有效,但是当我尝试使用 Guzzle 时,我收到一个 403 错误,说客户端拒绝了我的请求,这让我相信参数没有被正确传递。
这是我的 curl 代码:
// Sending Sms
$url = "https://api.xxxxxx.com/v3/messages/send";
$from = "xxxxxx";
$to = $message->getTo();
$client_id = "xxxxxx";
$client_secret = "xxxxxx";
$query_string = "?From=".$from."&To=".$to."&Content=".$content."&ClientId=".$client_id."&ClientSecret=".$client_secret."&RegisteredDelivery=true";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.$query_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
curl_close ($ch);
这是我现在的 Guzzle 代码:
$client = new Client();
$response = $client->request('GET', "https://api.xxxxxx.com/v3/messages/send", [
"From" => "xxxxxx",
"To" => $message->getTo(),
"Content" => $content,
"ClientId" => "xxxxxx",
"ClientSecret" => "xxxxxx",
"RegisteredDelivery" => "true"
]);
最佳答案
来自 the documentation :
所以只要让你的数组多维,将你的查询字符串参数放在数组的 query
元素中。
$client = new Client();
$response = $client->request('GET', "https://api.xxxxxx.com/v3/messages/send", [
"query" => [
"From" => "xxxxxx",
"To" => $message->getTo(),
"Content" => $content,
"ClientId" => "xxxxxx",
"ClientSecret" => "xxxxxx",
"RegisteredDelivery" => "true",
],
]);
关于php - 使用 Guzzle 发出带有 GET 参数的 HTTP 请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53326314/