How can I send cookies using PHP curl in addition to CURLOPT_COOKIEFILE?

后端 未结 4 1567
花落未央
花落未央 2020-11-27 16:31

I am scraping some content from a website after a form submission. The problem is that the script is failing every now and then, say 2 times out of 5 the script fails. I am

4条回答
  •  醉梦人生
    2020-11-27 16:57

    Here is a list of examples for sending cookies - https://github.com/andriichuk/php-curl-cookbook#cookies

    $curlHandler = curl_init();
    
    curl_setopt_array($curlHandler, [
    CURLOPT_URL => 'https://httpbin.org/cookies',
    CURLOPT_RETURNTRANSFER => true,
    
    CURLOPT_COOKIEFILE  => $cookieFile,
    CURLOPT_COOKIE => 'foo=bar;baz=foo',
    
    /**
     * Or set header
     * CURLOPT_HTTPHEADER => [
           'Cookie: foo=bar;baz=foo',
       ]
     */
    ]);
    
    $response = curl_exec($curlHandler);
    curl_close($curlHandler);
    
    echo $response;
    

提交回复
热议问题