Docker not responding on PHPUnit test - php

I am using Docker in my PHP project and everything has been working fine until now.
My API has an endpoint that needs to call another endpoint within the same API. So, doing a post to /hello will make, eventually a call to /x.
In order to make that call I do the following:
$client = new Client([
'base_uri' => $url,
'headers' => [
'Accept' => 'application/hal+json',
'Content-Type' => 'application/json',
],
]);
$response = $client->post('/x', [
'form_params' => [
'username' => $email,
'password' => $password,
],
]);
Where the URL is the exactly the same as the first one, but for the new endpoint. For example, if the POST is for: www.mydomain.com:8880/hello, the new call will be made to www.mydomain.com:8880/x.
Now, this is working fine with the exception of the tests. PHPUnit (which runs inside a docker container) starts its web server on localhost:8880, and when making that second call (so it will try to call localhost:8880/x) it will be hanging forever.
I've tried changing that second call to 127.0.0.1:8880/x, but that didn't work either.
Any ideas on what the problem is and how can it be fixed?
Thanks!

Related

Guzzle api call not working on droplet but working locally

Hi I'm trying to get a token from an api but no matter what I try on the droplet I get an invalid client every single time, the code is the same locally and on the droplet, currently working on local but not on the droplet.
This is the code
return Cache::rememberForever('payment_token', function () {
$client = new Client(['http_errors' => false]);
$params = [
'client_id' => env('CLIENT_ID'),
'client_secret' => env('CLIENT_SECRET'),
'grant_type' => 'client_credentials',
];
$headers = [
'Accept' => 'application/json',
];
$response = $client->request('POST', 'https://apipay.io/auth/token/', [
'json' => $params,
'headers' => $headers
]);
$res_body = json_decode($response->getBody()->getContents());
return $res_body->access_token;
});
The url for the post isn't a real one, I don't really think it's wise to post the real one as it doesn't work without the client_id and client_secret which I can't post here.
Is there a reason why the droplet would interfere with this? What can I do to fix this?
Double-check the remote .env file and make sure, that it's not some outdated, cached version of it (which env() would then return). Laravel has this feature, which can indeed be quite tricky, while not considering that (eg. it just doesn't work for no apparent reason). php artisan cache:clear clears the config-cache and php artisan config:cache builds it up again; I even think that production uses a cached config by default (which may be the actual difference there).

Trying to use bulksms.com API using guzzle in Laravel but returning errors

I have been trying to use guzzle for sending bulk sms from bulksms.com and it is returning this error,
guzzlehttp\exception\clientexception client error: post
https://api.bulksms.com/v1/messages resulted in a 401 full
authentication is required to access this resource response: : "type"
"https://developer.bulksms.com/json/v1/errors#authentication-failed
My code
$client = new Client([
'base_uri'=>'https://www.bulksms.com/',
'timeout'=>'900.0'
]);
//$result = $client->post('', [
// 'form_params' => [
// 'sample-form-data' => 'value'
// ]
//]);
$result = $client->request('POST','https://api.bulksms.com/v1/messages', [
'form_params' => [
'username' => 'username',
'password' => '****',
'sender' => 'my appname',
'recipients' => '+5555555555',
'message' => 'Testing message',
]
]);
Other people have already pointed you towards using authentication correctly, and using JSON as the format of your request. Additionally, you're using the wrong variable names. For example, the documentation uses the variable name to, and you have used recipients instead (maybe you copied and pasted that code from somewhere else?).
The documentation has a PHP code sample the uses curl, at: https://www.bulksms.com/developer/json/v1/#tag/Message - why not use that as a basis, and then convert it to a working Guzzle request, as a starting point?
Did you have a look at the Authentication section in the API docs?
You should authenticate with the API using HTTP Basic Auth.

Auth fails on Laravel Passport when calling it from Wordpress wp_remote_post, but works with Postman

I am re-asking and updating this question and deleting my old one as the comment went off track.
First time using Laravel Passport and I have spent hours trying to figure this out. I am calling an api endpoint from a Wordpress site. Both Passport and Laravel 5.7.19 are current versions from a clean install yesterday.
My API is working just fine using Postman, with Passport authentication.
I call a Laravel Passport API from Wordpress like this:
$args = array(
'method' => 'POST',
'timeout' => '45',
'redirection' => '5',
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(
'Accept' => 'application/json',
'X-Requested-With' => 'XMLHttpRequest',
'Authorization' => 'Bearer dd4b28b53ea...',
),
'body' => array(
'email' => $email,
'channel_url' => $channel_url,
'api_key' => $api_key,
),
'cookies' => array()
);
// PING API
$response = wp_remote_post( $login, $args );
If my route is inside the auth:api group like this:
Route::group([
'middleware' => 'auth:api'
], function() {
Route::post('my/route', 'Api\AuthController#userChannel');
});
It fails with 401 on the wp_remote_post() call. With Postman + Bearer token it works.
Taking the route outside of the auth:api group works for Wordpress and Postman and I can log / see the token:
[2019-02-11 05:23:32] local.INFo: Bearer dd4b28b53ea...
I have tried changes to .htaccess file and php artisan optimize:clear and Passport::withoutCookieSerialization(); in AppServiceProvider as suggested in numerous posts without success.
The exception at Laravel's side is:
The resource owner or authorization server denied the request. {"exception":"[object] (League\\OAuth2\\Server\\Exception\\OAuthServerException(code: 9): The resource owner or authorization server denied the request. at /home/vagrant/src/my-app/vendor/league/oauth2-server/src/Exception/OAuthServerException.php:215, InvalidArgumentException(code: 0): The JWT string must have two dots at /home/vagrant/src/my-app/vendor/lcobucci/jwt/src/Parser.php:95)
As Travis mentions there is nothing wrong with my code above. I had a logic issue in my controller. The code above works as it should.

ERROR : Guzzle Will not follow more than 1 redirects Drupal 8

i want to ask something confusing,
i had guzzle script in drupal 8 like this
$client = \Drupal::service('http_client');
$getUserInfo = $client->get($this->url, [
'Accept' => 'application/json',
'auth' => [
$this->authUsername,
$this->authPassword
]
]);
$resultUser = json_decode($getUserInfo->getBody());
i get error like this
error
but this is weird sometime this error missing but sometime it was appear, i dont know what is happen
and not just in that script but in every Guzzle request
i try
increase the maximum redirect to 1000 but its not work
disabled guzzle redirect but its stil not work
my analys
i test the API with POSTMAN everything is fine
and i had project with laravel in same server with the same API and its work
its just happen in drupal project in that server,
but in another server its fine
may someone had a same problem
Please advise
Thankyou
Have you tried to increase maximum redirects?
$client->get($this->url, [
'headers' => [
'Accept' => 'application/json',
],
'auth' => [
$this->authUsername,
$this->authPassword
],
'allow_redirects' => [
'max' => 5,
]
]);
P.S. Also you have a mistake with 'Accept': it should be inside the 'headers' container.

Guzzle Laravel - Login using POST request

What I am trying to do is to login to an external API and retrieve a JSON file. For this I am using Guzzle in Laravel.
I have setup a controller to do this:
$client = new Client([
'base_uri' => 'https://www.space-track.org',
'timeout' => 2.0,
]);
I access the JSON file using:
$response = $client->request('GET', '/basicspacedata/query/class/boxscore');
In order to get the JSON file I am required to login to the API. The API tutorial tells me:
Login by sending a HTTP POST request ('identity=your_username&password=your_password') to: https://www.space-track.org/ajaxauth/login
What I am unable to do is login to the API using Guzzle. I tried following a few Guzzle tutorials and using the 'auth' array to which none worked.
Basically, what I am unable to do is to login to the API using Guzzle.
Here is a basic flow that should work
// Initialize the client
$api = new Client([
'base_uri' => 'https://www.space-track.org',
'cookies' => true, // You have to have cookies turned on for this API to work
]);
// Login
$api->post('ajaxauth/login', [
'form_params' => [
'identity' => '<username>', // use your actual username
'password' => '<password>', // use your actual password
],
]);
// Fetch
$response = $api->get('basicspacedata/query/class/boxscore/format/json');
// and decode some data
$boxscore = json_decode($response->getBody()->getContents());
// And logout
$api->get('ajaxauth/logout');
dd($boxscore);
Now if it's not a one off request and you're planing on extensively using this API you can wrap this "ugliness" in your own service class that exposes a meaningful internal API that allows you to write then something along the lines of
$spaceTrack = new App\Services\SpaceTrack\Client();
$boxscore = $spaceTrack->getBoxscore();
dd($boxscore);

Categories