cURL returns 404 while the page is found in browser

前端 未结 4 780
误落风尘
误落风尘 2020-12-06 19:13

there is already similar questions on stackoverflow, but none of their solutions have been working for me. I\'m trying to grab a page on LoveIt.com with cURL, but it return

4条回答
  •  一整个雨季
    2020-12-06 19:45

    You don't need to save the cookie file via chrome.

    You can create a function to get this cookie, and then reuse it.

    Like:

    cookie     = (string) $cookie;
           $this->user_agent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0'; 
       }
    
       function get($url){
           $ch = curl_init();
           curl_setopt($ch, CURLOPT_URL, $this->url);
           curl_setopt($ch, CURLOPT_NOBODY, 1);
           curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
           // Here we create the file with cookies
           curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
           $this->http_response = curl_exec($ch);
       }
    
       function get_with_cookies($url){
           $ch = curl_init();
           curl_setopt($ch, CURLOPT_URL, $url);
           curl_setopt($ch, CURLOPT_NOBODY, 1);
           curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
           curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
    
           // Here we can re-use the cookie file keeping the save of the cookies 
           curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
           $this->http_response = curl_exec($ch);
        }
    }
    
    $crawler = new Crawler('cookie_file_name');
    // Creating cookie file
    $crawler->get('uri');
    // Request with the cookies
    $crawler->get_with_cookies('uri');
    

    Regards.

提交回复
热议问题