How to save cookie into a file and use that in other requests?
<p>I'm logging in to a page through Guzzle. It's saving the cookie. When I make subsequent requests it works perfectly fine. But, when I run the php again, I don't want php to do the same process which is logging in, getting the cookie again. So, I want to use existing cookie but I could not manage to do that. I don't think that it is well explained at <a href="http://docs.guzzlephp.org/en/latest/overview.html" rel="nofollow noreferrer">Guzzle Documentation</a>
Basically, steps must be like this:</p>

<ol>
<li>When the php runs for the first time, it will login to the url.
get the cookies. Save the cookies to the disk. Use it for subsequent
requests. </li>
<li>When php runs again, it must check if cookie exists or
    not. if not go to first step. If exists, use the cookie file for the requests.</li>
</ol>

<p>My class is as following. The problem here is, when php runs 2nd time, I need to log in again.</p>

<pre><code>&lt;?php
namespace OfferBundle\Tools;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\FileCookieJar;

class Example
{
private $site;
private static $client;
private static $cookieCreated = false;
private static $cookieExists;
private static $loginUrl = "http://website.com/account/login";
private static $header = [
    'Content-Type' =&gt; 'application/x-www-form-urlencoded',
    'User-Agent' =&gt; 'Mozilla/5.0 (Windows NT 10.0; WOW64)'
];
private static $cookieFile = 'cookie.txt';
private static $cookieJar;

private static $credential = array(
    'EmailAddress' =&gt; 'username',
    'Password'     =&gt; 'password',
    'RememberMe' =&gt; true
);

public function __construct($site) {
    self::$cookieExists = file_exists(self::$cookieFile) ? true : false;

    self::$cookieJar = new FileCookieJar(self::$cookieFile, true);
    self::$client = new Client(['cookies' =&gt; self::$cookieJar]);

    if(!self::$cookieCreated &amp;&amp; !self::$cookieExists) {
        self::createLoginCookie();
    }
    $this-&gt;site = $site;
}

public function doSth()
{
   $url = 'http://website.com/'.$this-&gt;site;
   $result = (String)self::$client-&gt;request('GET',$url, ['headers' =&gt; self::$header])-&gt;getBody();
    return $result;
}


private static function createLoginCookie()
{
    self::$client-&gt;request('POST', self::$loginUrl, [
        'form_params' =&gt; self::$credential,
        'connect_timeout' =&gt; 20,
        'headers' =&gt; self::$header
    ]);
    self::$cookieCreated = true;
}
</code></pre>

<p>Executed php:
    

<pre><code>use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use OfferBundle\Tools\Example;

class DefaultController extends Controller
{
/**
 * @Route("/")
 */
public function indexAction()
{

    $sm = new Example('anothersite.com');
    $result = $sm-&gt;doSth();
    dump($summary);

    die;
}
}
</code></pre>