0

I have a problem. Some time ago code bellow stoped working. Server returns 403 Forbidden nginx/1.8.0 response on a CURL request. Code work great on other domains. I can't solve this problem. Need your help.

private $referer;

private $useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) 
AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.65 Safari/525.19';

private $connect;

private $page;
private $last_open_url;

public function __construct( $with_cookies, $with_redirects, $max_timeout = 30 ) {
   $ch = curl_init();
   //curl_setopt($ch, CURLOPT_HEADER, true);
   curl_setopt($ch, CURLOPT_HEADER, 0);

   if($with_cookies) {
       curl_setopt($ch, CURLOPT_COOKIEFILE,  'cookiefile');
       curl_setopt($ch, CURLOPT_COOKIEJAR,  'cookiefile');

   }

   if ($with_redirects) {
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
   }

   curl_setopt($ch, CURLOPT_TIMEOUT, $max_timeout);
   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $max_timeout-1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   //curl_setopt($ch, CURLINFO_HEADER_OUT, true );
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

   curl_setopt($ch, CURLOPT_AUTOREFERER, true);
   curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);

   $this->connect = curl_copy_handle($ch);
} 

    public function go( $url, $post = false, $inc_ref = false )
{
    $ch_copy = curl_copy_handle($this->connect);

    if( $post !== false )
    {
        curl_setopt($ch_copy, CURLOPT_POST, 1);
        curl_setopt($ch_copy, CURLOPT_POSTFIELDS, $post);
    }

    if(!empty($this->referer))
        curl_setopt($ch_copy, CURLOPT_REFERER, $this->referer);

    if($inc_ref !== false)
        $this->referer = $url;

    curl_setopt($ch_copy, CURLOPT_URL, $url);

    $html = trim(curl_exec($ch_copy));
    curl_close($ch_copy);
    $this->last_open_url = $url;

    $this->page = $html;    
    return $html;
}

command

curl -Is *domain* | head -1

also 403 forbidden from different servers

  • maybe because a specific domain had been blocked from the origin – hassan Jun 04 '17 at 07:53
  • 4
    Possible duplicate of [Php Curl 403 forbidden error](https://stackoverflow.com/q/16915967/6521116) – LF00 Jun 04 '17 at 08:07
  • You can try with different agent. Example: `$useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';` Some servers do not accept the requests from scripts, it depends on user agents. – Milan Chheda Jun 04 '17 at 08:18
  • Try a latest browser string to make sure the site is just not imposing a "no legacy browsers" rule. Chrome 1 is pretty old. Mine is `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36` – apokryfos Jun 04 '17 at 08:30

0 Answers0