stefanosn
stefanosn

Reputation: 3324

Curl is giving me access denied

I use Curl -dump-header but i get also access denied. Is there any way to connect and get html code?

Curl --dump-header - https://www.gearbest.com/car-charger/pp_009363232829.html

> HTTP/2 403  server: AkamaiGHost mime-version: 1.0 content-type:
> text/html content-length: 314 cache-control: max-age=60 expires: Sat,
> 22 Jun 2019 22:25:51 GMT date: Sat, 22 Jun 2019 22:24:51 GMT
> set-cookie: AKAM_CLIENTID=7e3530d888ae97fef4ad26c997d733c5;
> expires=Mon, 31-Dec-2038 23:59:59 GMT; path=/; domain=.gearbest.com
> vary: User-Agent

<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>

You don't have permission to access "http&#58;&#47;&#47;www&#46;gearbest&#46;com&#47;car&#45;charger&#47;pp&#95;009363232829&#46;html" on this server.<P>
Reference&#32;&#35;18&#46;85451502&#46;1561242291&#46;25f21039
</BODY>
</HTML>

If i go to the page using the browser it works fine. I just want to get the html code of the page. any help appreciated.

Upvotes: 3

Views: 14279

Answers (1)

alx
alx

Reputation: 2367

Here is you PHP code modified, and it works perfectly fine (tested):

<?php
$curl1 = curl_init();
$url = "https://gearbest.com/car-charger/pp_009363232829.html";
curl_setopt($curl1, CURLOPT_URL, $url);
curl_setopt($curl1, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($curl1, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0');
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_FOLLOWLOCATION, true);
$str = curl_exec($curl1); 
echo $str;

Few notes:

  • you were expecting curl_exec() to return HTML code, but without setting CURLOPT_RETURNTRANSFER option first that would not happen
  • often sites use redirects (e.g. GearBest redirects to www. if you omit it), and to handle that properly you need to set CURLOPT_FOLLOWLOCATION option

Upvotes: 1

Related Questions