0

I have test curl command on Reqbin.com and get correct result.

curl execed on reqbin return exactly result

But when I run php curl code, It return Access Denied error.

Result when run php curl code

This is my code:

<?php

$url = "https://www.freepik.com/premium-vector/green-sketches-vegetables-background_899769.htm";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   'if-none-match: W/"49cmjzwjsg4y8b"',
   "priority: u=0, i",
   "sec-ch-ua-mobile: ?0",
   "sec-fetch-site: none,",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

?>

What is wrong? Thanks!

8
  • 5
    Why should I not upload images of code/data/errors?. Code, data and logs are text. Provide it as text, please. See also How to Ask where this is mentioned. Images of text are not searchable, copyable, or re-usable in answers, examples, research etc. This makes it a lot harder for people to help you by re-using your code or data in a test scenario or an answer, and for others with similar issues/errors to find your question. You can edit your post. Thanks.
    – ADyson
    Commented Apr 15 at 10:26
  • Have you tested the code locally? It seems that the response is from reqbin. They don't seem to allow you to make requests to random websites from PHP
    – JensV
    Commented Apr 15 at 10:29
  • 1
    @JensV not sure why you think the response was from reqbin? "They don't seem to allow you to make requests to random websites from PHP" - but if using cURL directly, they do ...? Not sure how that would make much sense.
    – C3roe
    Commented Apr 15 at 10:37
  • 1
    Could be that your PHP version sends additional headers, and one the target doesn't like. Send both requests to a service like requestcatcher.com, and compare.
    – C3roe
    Commented Apr 15 at 10:39
  • 5
    Note that Freepik offers an API: freepik.com/api#stock-content If you're currently planning on doing any kind of scraping of the actual website, I'm pretty sure that it's against their TOS
    – JensV
    Commented Apr 15 at 10:48

1 Answer 1

2

The cURL command line comes with a default user agent, adding it to the PHP code can successfully get a return:

curl_setopt($curl, CURLOPT_USERAGENT, 'curl/8.9.1');

BTW your "sec-fetch-site: none," header contains an extra comma.

1
  • That's so great! It worked fine. Thanks in advance. Commented Apr 16 at 1:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.