PHP卷曲请求响应403 - 未经授权

时间:2017-12-13 10:04:29

标签: php curl

我试图在域https://www.submarino.com.br中运行PHP curl GET 。我得到了403(Forbidden)。然后我尝试使用chrome developper复制url中提供的curl请求并在bash中运行并得到相同的答案

我的PHP代码

public function GetStatusHost(){
    $headers   = Array();
    $headers[] = 'Host:www.submarino.com.br';
    $headers[] = 'Connection:keep-alive';
    $headers[] = 'Cache-Control:max-age=0';
    $headers[] = 'Upgrade-Insecure-Requests: 1';
    $headers[] = 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8';
    $headers[] = 'Accept-Language:pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7,es;q=0.6,pt-PT;q=0.5';
    $headers[] = 'Accept-Encoding:gzip, deflate, br';
    $headers[] = "If-None-Match:W/'5beff-TgqN41ZtNiOTyAH2bpA4lvYiKTE'";

    $ch        = curl_init($this->GetKeywordUrl());
    $tmp       = "/home/leilao/public_html/tmp/curl_cookie/cookie.txt";
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    //curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp);
    //curl_setopt($ch, CURLOPT_COOKIEFILE,$tmp);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    $result = curl_exec($ch);
    $info      = curl_getinfo($ch);
    curl_close($ch); echo $info['http_code']; exit;
    return $info['http_code'];
}

Bash

enter image description here

任何想法如何解决这个问题?

使用标题的响应SomeHugeOAuthaccess_tokenThatIReceivedAsAString:

enter image description here

1 个答案:

答案 0 :(得分:1)

也许您的网站(Host:www.submarino.com.br)使用身份验证凭据来提供方法访问权限。 因此,请尝试使用特定凭据添加此行:

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "username:password"); //Your credentials goes here

或     如果使用base64string授权,那么您也可以使用

$header[] = "Authorization: Bearer $access_token";

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization', 'OAuth ' . $atoken));