Home > Net >  How to set cookies for an backend API call with laravel
How to set cookies for an backend API call with laravel

Time:10-31

So I am making an application for myself where I have a price history graph of an single steam market item. But the API I am calling needs a cookie called steamLoginSecure. There is an example on how to do this in Pyton (How to retrieve steam market price history?). But I want to do this in Laravel as I am building my application in Laravel.

This is the API link that I am trying to call: https://steamcommunity.com/market/pricehistory/?country=PT&currency=3&appid=730&market_hash_name=Falchion Case

What I have tried so far:

    Route::get('/getPriceHistory/{marketHashName}', function ($marketHashName) {
    $cookie = Cookie::make('steamLoginSecure', 'xxx', 3600);
    return Http::get("https://steamcommunity.com/market/pricehistory/?country=US&currency=1&appid=252490&market_hash_name=Fish Hammer")->headers->setCookie($cookie)->json();
    });

And

Route::get('/getPriceHistory/{marketHashName}', function ($marketHashName) {
    return Http::get("https://steamcommunity.com/market/pricehistory/?country=US&currency=1&appid=252490&market_hash_name=Fish Hammer")->withCookie(cookie('steamLoginSecure', 'xxx', 3600))->json();
});

xxx represents the token this is steam user based.

CodePudding user response:

You can use Guzzle and its methods:

use GuzzleHttp\Cookie\CookieJar;

$cookieJar = CookieJar::fromArray([
    'steamLoginSecure' => 'xxx'
], 'steamcommunity.com');

$url = 'https://steamcommunity.com/market/pricehistory/?country=US&currency=1&appid=252490&market_hash_name=Fish Hammer';

$response = Http::withOptions([
    'cookies' => $cookieJar
])->get($url);

dd($response->json());

  • Related