0

I have the following script:

$curl = curl_init();
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_URL, 'http://4pda.ru/forum/index.php?act=idx');
curl_setopt($curl, CURLOPT_HEADER, true);

$headers = array();
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; 
$headers[] = 'Connection: Keep-Alive'; 
$headers[] = 'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3';
$headers[] = 'Accept-Encoding: gzip, deflate';
$headers[] = 'Host: 4pda.ru';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0';


curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$cookie_path = "./cookies.txt";
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_path);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);

$result = curl_exec($curl); 
var_dump(curl_getinfo($curl,CURLINFO_HEADER_OUT)); //problem one

curl_close($curl);
var_dump($result);

I have two problems. First, PHP Curl does not set headers. It sends only these headers:

User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0
Host: 4pda.ru
Accept: */*

You can see that the php set headers incorrectly. The second problem is that the server returns the following content:

HTTP/1.1 403 Forbidden
Server: nginx
Date: Wed, 26 Jun 2013 14:26:18 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Keep-Alive: timeout=20

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>

Although, I expect to get a html of page.

It is surprising that this script works fine on my home server (windows), and on my previous server. I think that my problems are due to incorrectly configure a new server. But I could be wrong.

I would be glad to any advice. Regards, Denis.

Update. Thanks Ma Chuan, the first problem is solved.

1 Answer 1

2

Didn't you use a wrong variable name in that set header line?

curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);

should read

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for what you have found my typo. The first problem was solved. Unfortunately, the second problem is urgent.
In your request header example, I don't see the cookie header. Does the server require sending cookie for authorization?
no, you can go to 4pda.ru/forum/index.php?act=idx and get the content without cookies.
I ran your script and it worked as expected. It's possible that 4pda.ru only rejects requests from your new server's ip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.