Guzzle 7 + Guzzle-cache-middleware - caching

I'm using Guzzle 7 to grab content from an external API with basic authentication. It works fine. Now I'd like to integrate cache management. So i've tried to use this plugin: [Guzzle-cache-middleware][1] and I can't make it working correctly. I can get API response and my desired content but the cache directory is not populated.
I've searched all around the web but I can't figure this out. Could you please tell me how to solve this? Here is my code:
$userName = "xxxxxxx";
$password = "yyyyyyyyyyy";
require_once './vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy;
$stack = HandlerStack::create();
$cache = new CacheMiddleware();
$stack->push($cache, '/home/xxxx/xxxxx/guzzle2/cache');
$client = new Client([
'handler' => $stack,
'base_uri' => 'https://api.xxxxx.com/xxx/',
"timeout" => 30.0,
]);
$json = $client->get('zzzzzz.json', [
'auth' => [
$userName,
$password
]
]);
var_dump($json->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO));
Output:
string(4) "MISS"
So API result is different from cache. But headers params (ETag and Last-Modified) are still unchanged and my cache folder is still blank.
Thank you for your help!

Related

how can i send http post requests from Laravel to Strapi CMS via Strapi's API

I tried sending post requests to the API in postman and it works fine. but i can't find a proper syntax of doing from Laravel, i tried following this tutorial from the Laravel documentation Http-client so i tried this syntax
function sendRequest (Request $data) {
$phonenumber = $data['number'];
$name = $data['name'];
$data2['data']['attributes'] = [
[
'phonenumber' => $phonenumber,
'name' => $name
]
];
Http::post('http://example.com/users', $data2);
}
didn't work.
here is the API's data structure
thanks in advance
I do not know if this will make a difference but you're missing a comma after value2 as per the syntax.
$response = Http::post('http://example.com/users', [
'name' => 'Steve',
'role' => 'Network Administrator', ]);
I'm rather new to laravel also and id love to know this fix too incase I need it in the future :D good luck!

Needing to add a dynamic job Id to the uri using Guzzle 7

Im passing in a job Id that dynamically needs to populate the uri in guzzle 7 here is the client:
$client = new GuzzleHttp\Client([
'base_uri' => 'https://portal.2nformserver.com/arcgis/rest/services/getLocalRefData/GPServer/Get%20Local%20Reference%20Data/',
['headers' => [
'Accept' =>'application/json'
]]
]);
here is the promise:
$promise = $client->requestAsync('GET', 'jobs/', ['query' => [
"f"=>"json"
]]);
I am needing to pass in a $jobId variable into the Uri portion of the request. I have not been able to find a way to do this. I was hoping for something as easy as:
$promise = $client->requestAsync('GET', 'jobs/{$jobId}', ['query' => [
"f"=>"json"
]]);
or to be able to add something to the array I could pass in but have come up with no answers any help would be greatly appreciated.
you can pass variable to uri using php string format:
$promise = $client->requestAsync('GET', "jobs/$jobId", ['query' => [
"f"=>"json"
]]);

Laravel GuzzleHttp response empty

I'm trying to retrieve the value of a URL but it returns a null response. Not sure what I'm doing wrong I've been trying to retrieve the value but I get an empty value. Below is my code
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'https://fantasy.premierleague.com/api/bootstrap-static/');
dd($response->getBody()->getContents());
When trying to dump the response I get the below response
When trying to read the getBody() of the response I get this output
I'm using guzzle "guzzlehttp/guzzle": "^6.4"
OK I found the solution to my problem. It's got nothing to do with
$response->getBody()->getContents()
But the problem was the Endpoint/URL might require a user agent as part of the parameter of the url
my code I was able to retrieve the value using the code below
$url = 'https://fantasy.premierleague.com/api/bootstrap-static/';
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', $url, [
'verify' => false,
'headers' => [
'User-Agent' => 'CUSTOM_AGENT_YOU_WANT' // THIS IS WHAT I ADDED TO MAKE IT WORK
]
]);
dd(json_decode($response->getBody()->getContents(), true));

File download in headless chrome using Laravel/Dusk

I'm trying to automate a file download in headless chrome using Laravel/Dusk.In GUI mode,the file downloads just fine in my download folder.But in headless mode,the download does not take place at all.Is there any way to solve this issue?
For those who come across this, I found a simple solution with the current version of Laravel at the time of writing this.
I suggest first creating a directory in your storage path called temp (probably want to gitignore this too), and then navigate to the DuskTestCase.php file setup with the Dusk installation.
Under the driver method, add the following under the section that initializes the ChromeOptions variable.
$options->setExperimentalOption('prefs', [
'download.default_directory' => storage_path('temp')
]);
The driver function should now look like this:
$options = (new ChromeOptions())->addArguments([
'--disable-gpu',
'--headless',
'--window-size=1920,1080'
]);
$options->setExperimentalOption('prefs', [
'download.default_directory' => storage_path('temp')
]);
return RemoteWebDriver::create(
'http://localhost:9515',
DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY,
$options
)
);
As a side note, this worked for me with a PDF file created via JS, so I can't definitively say how this works with a file downloaded from the back-end.
public function testDownload($account){
$this->browse(function (Browser $browser) {
$download_path = storage_path('your/download/path');
$url = $browser->driver->getCommandExecutor()->getAddressOfRemoteServer();
$uri = '/session/' . $browser->driver->getSessionID() . '/chromium/send_command';
$body = [
'cmd' => 'Page.setDownloadBehavior',
'params' => ['behavior' => 'allow', 'downloadPath' => $download_path]
];
(new \GuzzleHttp\Client())->post($url . $uri, ['body' => json_encode($body)]);
// Start your test
$browser->visit("http://example.com/export")
//your asserts here
}
It's not necessary to trigger a separate Guzzle POST, I used a CustomWebDriverCommand instead:
$command = new \Facebook\WebDriver\Remote\CustomWebDriverCommand(
$driver->getSessionID(),
"/session/:sessionId/chromium/send_command",
"POST",
[
"cmd" => "Page.setDownloadBehavior",
"params" => ["behavior" => "allow", "downloadPath" => '/your/download/path']
]
);
$driver->getCommandExecutor()->execute($command);

Https urls in Laravel - Rackspace with Filesystem

I am using filesystem package without problems for upload and download files to rackspace. According to docs on how to obtain the file url I do:
$content = fopen('logo.png', 'r+');
\Storage::disk('disk_temp')->put('logos/logo.png', $content);
$url = \Storage::disk('disk_temp')->url('logos/logos.png');
Works good. But, this url is http:// version. How to obtain the secure version like https:// if I am using rackspace vendor? Here my config:
'disk_temp' => [
'driver' => 'rackspace',
'username' => env('RS_USER'),
'key' => env('RS_API'),
'container' => 'disk_temp',
'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/',
'region' => env('RS_REGION', 'DFW'),
'url_type' => 'publicURL',
],
Years before, I used Open Cloud successfully, obtaining the URL as
$file = \OpenCloud::container('cdn')->uploadObject($filename, $content);
$url = (string) $file->getPublicUrl(UrlType::SSL);
Just to know if possible doing the same from laravel itself. Thank you in advance.

Resources