2015-08-07 16 views
6

Başka bir PHP API'sine istekte bulunan bir PHP webapp'ım var. $_COOKIES dizisini $options['cookies']'a ileterek http isteklerini yapmak için Guzzle kullanıyorum. Bunu yapıyorum çünkü API, ön uygulama olarak aynı Laravel oturumunu kullanıyor. Yakın zamanda Guzzle 6'ya geçtim ve $_COOKIES'u $options['cookies']'a (CookieJar atamakla ilgili bir hata alıyorum) iletemiyorum. Benim sorum, tarayıcımda hazırladığım çerezleri Guzzle 6 istemci örneğime nasıl uygulayabilirim?Çerezleri tarayıcıdan Guzzle'a aktarma 6 istemci

cevap

5

gibi bir şey deneyin:

/** 
* First parameter is for cookie "strictness" 
*/ 
$cookieJar = new \GuzzleHttp\Cookie\CookieJar(true); 
/** 
    * Read in our cookies. In this case, they are coming from a 
    * PSR7 compliant ServerRequestInterface such as Slim3 
    */ 
$cookies = $request->getCookieParams(); 
/** 
    * Now loop through the cookies adding them to the jar 
    */ 
foreach ($cookies as $cookie) { 
      $newCookie =\GuzzleHttp\Cookie\SetCookie::fromString($cookie); 
      /** 
      * You can also do things such as $newCookie->setSecure(false); 
      */ 
      $cookieJar->setCookie($newCookie); 
} 
/** 
* Create a PSR7 guzzle request 
*/ 
$guzzleRequest = new \GuzzleHttp\Psr7\Request(
        $request->getMethod(), $url, $headers, $body 
     ); 
/** 
    * Now actually prepare Guzzle - here's where we hand over the 
    * delicious cookies! 
    */ 
$client = new \GuzzleHttp\Client(['cookies'=>$cookieJar]); 
/** 
    * Now get the response 
    */ 
$guzzleResponse = $client->send($guzzleRequest, ['timeout' => 5]); 

ve burada tekrar çıkmak için:

$newCookies = $guzzleResponse->getHeader('set-cookie'); 

yardımcı olur Umut!