Guzzle Post Null/Empty Values in Laravel - laravel

I've been trying to work with Guzzle and learn my way around it, but I'm a bit confused about using a request in conjunction with empty or null values.
For example:
$response = $client->request('POST',
'https://www.testsite.com/coep/public/api/donations', [
'form_params' => [
'client' => [
'web_id' => NULL,
'name' => 'Test Name',
'address1' => '123 E 45th Avenue',
'address2' => 'Ste. 1',
'city' => 'Nowhere',
'state' => 'CO',
'zip' => '80002'
],
'contact' => [],
'donation' => [
'status_id' => 1,
'amount' => $donation->amount,
'balance' => $donation->balance,
'date' => $donation->date,
'group_id' => $group->id,
],
]
]);
After running a test, I found out that 'web_id' completely disappears from my request if set to NULL. My question is how do I ensure that it is kept around on the request to work with my conditionals?
At this point, if I dd the $request->client, all I get back is everything but the web_id. Thanks!

I ran into this issue yesterday and your question is very well ranked on Google. Shame that it has no answer.
The problem here is that form_params uses http_build_query() under the hood and as stated in this user contributed note, null params are not present in the function's output.
I suggest that you pass this information via a JSON body (by using json as key instead of form_params) or via multipart (by using multipart instead of form_params).
Note: those 2 keys are available as constants, respectively GuzzleHttp\RequestOptions::JSON and GuzzleHttp\RequestOptions::MULTIPART

Try to define anything like form_params, headers or base_uri before creating a client, so:
// set options, data, params BEFORE...
$settings = [
'base_uri' => 'api.test',
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'ajustneedatokenheredontworry'
],
'form_params' => [
'cash' => $request->cash,
'reason' => 'I have a good soul'
]
];
// ...THEN create your client,
$client = new GuzzleClient($settings);
// ...and finally check your response.
$response = $client->request('POST', 'donations');
If you check $request->all() at the controller function that you are calling, you should see that were sent successfully.

For those using laravel, use this:
Http::asJson()

Related

Send nested array via Laravel Http Client

I want to send request to an API website containing nested params via Laravel Http Client (Laravel 7.x)
$params = [
'type' => 'books',
'variables' => [
'color' => 'red',
'size' => 'large'
]
]
I want my url to like this:
http://example.com/?type=books&variables={"color":"red","size":"large"}
encoded above url:
http://example.com/?type=books&variables=%7B%22color%22%3A%22red%22%2C%22size%22%3A%22large%22%7D
But when I use:
Http::get('http://example.com', $params);
The API server returns error.
But when I use:
Http::get('http://example.com/?type=books&variables={"color":"red","size":"large"}');
It works well.
So how can I convert my params array into url ?
(I don't have access to API server)
try json_encode()
$params = [
'type' => 'books',
'variables' => json_encode([
'color' => 'red',
'size' => 'large'
])
]
$url = "http://example.com?".http_build_query($params);
Http::get($url);
and http_build_query()
ref link https://www.php.net/manual/en/function.http-build-query.php

Stripe webhook test returns empty table on execution, but works on stripe's webhook tester

We're having a problem with our test suite.
When we run this using the test suite we get a 'The table is empty...' response from PHPUnit.
We know it works as we've also tested using Stripe's 'Send a web hook' test function which works, and the response is stored as expected.
Our code is here:
public function test_webhook_received()
{
$this->expectsJobs([StoreStripeWebHookJob::class]);
$this->postJson('/stripeHook', [
'created' => 1326853478,
'livemode' => false,
'id' => 'evt_00000000000000',
'type' => 'account.external_account.created',
'object' => 'event',
'request' => NULL,
'pending_webhooks' => 1,
'api_version' => '2019-12-03',
'data' => [
'object' => [
'id' => 'ba_00000000000000',
'object' => 'bank_account',
'account' => 'acct_00000000000000',
'account_holder_name' => 'Jane Austin',
'account_holder_type' => 'individual',
'bank_name' => 'STRIPE TEST BANK',
'country' => 'US',
'currency' => 'gbp',
'fingerprint' => '8JXtPxqbdX5GnmYz',
'last4' => '6789',
'metadata' => [],
'routing_number' => '110000000',
'status' => 'new',
],
],
]);
$this->assertDatabaseHas('stripe_webhooks', [
'stripe_created_at' => 1326853478,
'type' => 'account.external_account.created',
]);
}
The response received is:
Failed asserting that a row in the table [stripe_webhooks] matches the
attributes {
"stripe_created_at": 1326853478,
"type": "account.external_account.created" }.
The table is empty..
If we remove the
$this->expectsJobs([StoreStripeWebHookJob::class]);
tests succeed. Obviously the expectsJob() call should be where it is though.
ExpectsJob also intercepts the job. Much like expectsException. Judging from your clean naming convention "StoreStripe..." - I'd say it's really not storing under these test circumstances.
You'll need to test separately that your endpoint/controller is queuing a job... and that the job is storing the data. 2 tests.

Nexmo Laravel connect to PSTN endpoint not working

My code is shown below. Everything works for me, besides connecting a call to a PSTN endpoint. I'm thinking possibly that maybe the "connect" functions aren't included in the Laravel Nexmo packages that I'm using. I'm using these:
https://github.com/Nexmo/nexmo-laravel
Which is built on another nexmo package:
https://github.com/Nexmo/nexmo-php
My code:
public function getNexmoAnswer(Request $request)
{
return
[
[
'action' => 'talk',
'voiceName' => 'Justin',
'text' => 'Welcome to our great site’
],
[
'action' => 'talk',
'voiceName' => 'Justin',
'text' => "Press # to search now the e.t.a status of your latest order.",
'bargeIn' => true
],
[
'action' => 'connect',
'from' => '17181234567’,
'endpoint' => [
'type' => 'phone',
'number' => '18451234567’
]
]
];
}
The connect action is part of Nexmo's NCCO system and is unrelated to Nexmo-Laravel and Nexmo-PHP. Your NCCO looks to be correct to me.
To connect to another number, you'll need to have activated your account by adding credit. In addition, the from number you're using (17181234567) must be a number purchased from Nexmo

How to post a new context to my wit bot?

I'm writing a bot using Wit.ai and I'm having trouble understanding context. More specifically how/when to set it. As I've understood it you set the context by posting to the API at any point in time you see fit, for example when executing a function defined in a wit story. At least that's the approach I'm aiming for with the code below. Sadly it generates an error though so I wonder, am I trying to set the context in an incorrect way or is there something wrong with my Guzzle post?
private function storeUserName($entities) {
$witcall = $this->wit->post($this->wit_base_url.'/converse',
[
'form_params' => [
'username' => $entities->contact[0]->value
],
'query' => [
'v' => '1',
'session_id' => 'vk-'.$this->thread_id
],
'headers' => [
'Authorization' => 'Bearer '.env('WIT_TOKEN', false)
]
]
);
return [
'msg' => 'nice',
'type' => 'msg'
];
}
My error:
ClientException in RequestException.php line 111:
Client error: `POST https://api.wit.ai/converse?v=1&session_id=vk-1` resulted in a `400 Bad Request` response:
Unable to parse context in body
As it turned out using form_params was incorrect, changed to json and now it works fine. Like this:
$call = $this->wit->request('POST', 'converse', [
'json' => $this->context,
'query' => [
'v' => '1',
'session_id' => 'vk-'.$this->thread_id
]
]);

sending form data via PHP

I'm trying to submit a form using PHP and the mailchimp 2.0 api.
I'm getting an error that says:
FNAME must be provided
My form has a field for the first name:
<input type="text" name="fname">
So it must have something to do with the way I am handling it the php side.
Here is the bit of php that handles FNAME:
$result = $MailChimp->call('lists/subscribe', array(
'id' => 'myid',
'email' => array( 'email' => $_POST['email']),
'FNAME' => $_POST['fname'],
'LNAME' => $_POST['lname'],
'double_optin' => false,
'update_existing' => true,
'replace_interests' => false
));
I'm not sure if I'm forming the array correctly or not.
By the way, I'm using this wrapper, but I think my error has to do with how I create $result and not the wrapper.
https://github.com/drewm/mailchimp-api
Any help would be appreciated.
Thanks!
Peep the example at the bottom of the page you linked to. I've pasted it here:
$result = $MailChimp->call('lists/subscribe', array(
'id' => 'b1234346',
'email' => array('email'=>'davy#example.com'),
'merge_vars' => array('FNAME'=>'Davy', 'LNAME'=>'Jones'),
'double_optin' => false,
'update_existing' => true,
'replace_interests' => false,
'send_welcome' => false,
));
print_r($result);
Your merge vars (ex. FNAME and LNAME) need to be in its own array. So, add a 'merge_vars' in your array and create an array that contains your field's merge variables.

Resources