John Davidson

php - Curl is giving me an error and only removing CURLOPT_USERAGENT and CURLOPT_HTTPHEADER it works

0 comments
Message:


I'm trying to make this to work, but it only works if I remove the two lines that are commented out. If I enable it stops working with a Error 404 (Not Found). Why?


Thanks in advance.


$headers = [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding: gzip, deflate, br',
'Accept-Language: pt-PT,pt;q=0.9,en-US;q=0.8,en;q=0.7',
'Cache-Control: no-cache',
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'Host: httpbin.org',
'Referer: http://www.google.com',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'X-MicrosoftAjax: Delta=true',
'X-Amzn-Trace-Id: Root=1-60e1dc05-44eb1f0a7cff152139d79c76'
];

$url = $_POST['URL'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$get = curl_exec($ch);


My Answer:
The issue you are facing could be due to the way you are setting the CURLOPT_REFERER and CURLOPT_HTTPHEADER options in your cURL request.

When you set the CURLOPT_REFERER option, you should use CURLOPT_REFERER instead of CURLOPT_REFERER. So the correct way to set the referer header would be:

php
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');


Similarly, when setting the CURLOPT_HTTPHEADER option, you should use CURLOPT_HTTPHEADER instead of CURLOPT_HTTPHEADER. So the correct way to set the headers would be:

php
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);


By making these changes, your cURL request should work as expected with the user agent and custom headers included.

Rate this post

3 of 5 based on 4262 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us