Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Guzzle to make HTTP requests with GET parameters

Tags:

php

guzzle

I'm trying to use Guzzle to make a request from my server, not a requirement. I use cURL and that works, however when I try with Guzzle I get a 403 error saying the client rejected my request which makes me believe the params are not being passed correctly.

This is my cURL code:

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

This is my Guzzle code now:

$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"
]);
like image 804
user3718908x100 Avatar asked Mar 18 '26 18:03

user3718908x100


1 Answers

From the documentation:

You can specify the query string parameters using the query request option as an array.

$client->request('GET', 'http://httpbin.org', [
    'query' => ['foo' => 'bar']
]);

So just make your array multidimensional, placing your query string parameters in the query element of the array.

$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",
    ],
]);
like image 187
miken32 Avatar answered Mar 21 '26 08:03

miken32



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!