Php curl http error code

Why am I getting HTTP_CODE 0 from curl?

I have been using curl with PHP for a while. Today I’ve been trying to fetch http://www.webhostingstuff.com/category/Best-Hosting.html and I keep getting http code 0, which is new to me. I set the headers

$s->headers = array( "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language: en-gb,en;q=0.5", "Accept-Encoding: gzip, deflate", "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7", "Keep-Alive: 115", "Connection: keep-alive", "Referer: https://google.com" ); 

and I have a cookie file (which has nothing in it when the script finishes loading) Here’s the curl function

function fetch($url, $username='', $data='', $proxy='') < $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, true); if(isset($proxy)) < curl_setopt($ch,CURLOPT_TIMEOUT,30); curl_setopt($ch, CURLOPT_PROXY, $proxy); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); curl_setopt($ch, CURLOPT_PROXYPORT, $proxy); curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'proxyadmin:parola'); >curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FRESH_CONNECT,true); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); if(!empty($username)) < curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie/.txt"); curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie/.txt"); > curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); if (is_array($data) && count($data)>0) < curl_setopt($ch, CURLOPT_POST, true); $params = http_build_query($data); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); >if (is_array($this->headers) && count($this->headers)>0)< curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); > $this->result = curl_exec($ch); $curl_info = curl_getinfo($ch); $header_size = $curl_info["header_size"]; $this->headers = substr($this->result, 0, $header_size); $this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $this->error = curl_error($ch); curl_close($ch); > 
[brian@ip-184-168-22-244 ~]$ curl -url http://www.webhostingstuff.com/addcomments/5ite.html Enter host password for user 'rl': curl: (7) couldn't connect to host [brian@ip-184-168-22-244 ~]$

Источник

Получаем HTTP статус-коды сайта с помощью PHP и CURL

Используя нижеприведенный код вы сможете проверить, существует сайт или нет. Также можно проверить, есть ли на сайте редирект. Это может быть полезно для сайтов-каталогов, которые хотите проверить урлы, которые больше не являются активными или обновить свои ссылки. С помощью CURL мы получаем все статус коды для какого либо сайта, а затем ищем совпадения со списком HTTP статус-кодов.

Читайте также:  Javascript удалить все cookies

$toCheckURL = «http://google.com» ; // Домен для проверки

curl_setopt( $ch , CURLOPT_URL, $toCheckURL );

curl_setopt( $ch , CURLOPT_HEADER, true);

curl_setopt( $ch , CURLOPT_NOBODY, true);

curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true);

curl_setopt( $ch , CURLOPT_FOLLOWLOCATION, true);

curl_setopt( $ch , CURLOPT_MAXREDIRS, 10); // разрешаем только 10 редиректов за раз во избежание бесконечного цикла

$http_code = curl_getinfo( $ch , CURLINFO_HTTP_CODE); // Получаем HTTP-код

$new_url = curl_getinfo( $ch , CURLINFO_EFFECTIVE_URL);

// Массив возможных HTTP статус кодовв

$codes = array (0=> ‘Domain Not Found’ ,

203=> ‘Non-Authoritative Information’ ,

407=> ‘Proxy Authentication Required’ ,

413=> ‘Request Entity Too Large’ ,

415=> ‘Unsupported Media Type’ ,

416=> ‘Requested Range Not Satisfiable’ ,

500=> ‘Internal Server Error’ ,

505=> ‘HTTP Version Not Supported’ );

// Ищем совпадения с нашим списком

if (isset( $codes [ $http_code ]))

echo ‘Сайт вернул ответ: ‘ . $http_code . ‘ — ‘ . $codes [ $http_code ]. ‘
‘ ;

preg_match_all( «/HTTP/1.[1|0]s(d)/» , $data , $matches );

// Идем дальше по списку, чтобы посмотреть, какие мы еще статус коды получили

// Проверяем если урл поменялся или нет

Источник

curl_getinfo

// Check HTTP status code
if (! curl_errno ( $ch )) switch ( $http_code = curl_getinfo ( $ch , CURLINFO_HTTP_CODE )) case 200 : # OK
break;
default:
echo ‘Unexpected HTTP code: ‘ , $http_code , «\n» ;
>
>

// Close handle
curl_close ( $ch );
?>

Notes

Note:

Information gathered by this function is kept if the handle is re-used. This means that unless a statistic is overridden internally by this function, the previous info is returned.

User Contributed Notes 13 notes

Here are the response codes ready for pasting in an ini-style file. Can be used to provide more descriptive message, corresponding to ‘http_code’ index of the arrray returned by curl_getinfo().
These are taken from the W3 consortium HTTP/1.1: Status Code Definitions, found at
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Читайте также:  Map meaning in java
[Informational 1xx]100=»Continue»
101=»Switching Protocols» [Successful 2xx]200=»OK»
201=»Created»
202=»Accepted»
203=»Non-Authoritative Information»
204=»No Content»
205=»Reset Content»
206=»Partial Content» [Redirection 3xx]300=»Multiple Choices»
301=»Moved Permanently»
302=»Found»
303=»See Other»
304=»Not Modified»
305=»Use Proxy»
306=»(Unused)»
307=»Temporary Redirect» [Client Error 4xx]400=»Bad Request»
401=»Unauthorized»
402=»Payment Required»
403=»Forbidden»
404=»Not Found»
405=»Method Not Allowed»
406=»Not Acceptable»
407=»Proxy Authentication Required»
408=»Request Timeout»
409=»Conflict»
410=»Gone»
411=»Length Required»
412=»Precondition Failed»
413=»Request Entity Too Large»
414=»Request-URI Too Long»
415=»Unsupported Media Type»
416=»Requested Range Not Satisfiable»
417=»Expectation Failed» [Server Error 5xx]500=»Internal Server Error»
501=»Not Implemented»
502=»Bad Gateway»
503=»Service Unavailable»
504=»Gateway Timeout»
505=»HTTP Version Not Supported»

And an example usage:
$ch = curl_init (); // create cURL handle (ch)
if (! $ch ) die( «Couldn’t initialize a cURL handle» );
>
// set some cURL options
$ret = curl_setopt ( $ch , CURLOPT_URL , «http://mail.yahoo.com» );
$ret = curl_setopt ( $ch , CURLOPT_HEADER , 1 );
$ret = curl_setopt ( $ch , CURLOPT_FOLLOWLOCATION , 1 );
$ret = curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 0 );
$ret = curl_setopt ( $ch , CURLOPT_TIMEOUT , 30 );

// execute
$ret = curl_exec ( $ch );

if (empty( $ret )) // some kind of an error happened
die( curl_error ( $ch ));
curl_close ( $ch ); // close cURL handler
> else $info = curl_getinfo ( $ch );
curl_close ( $ch ); // close cURL handler

if (empty( $info [ ‘http_code’ ])) die( «No HTTP code was returned» );
> else // load the HTTP codes
$http_codes = parse_ini_file ( «path/to/the/ini/file/I/pasted/above» );

// echo results
echo «The server responded:
» ;
echo $info [ ‘http_code’ ] . » » . $http_codes [ $info [ ‘http_code’ ]];
>

Источник

Getting HTTP code in PHP using curl

I’m using CURL to get the status of a site, if it’s up/down or redirecting to another site. I want to get it as streamlined as possible, but it’s not working well.

Читайте также:  Python try exception type

I have this wrapped in a function. It works fine but performance is not the best because it downloads the whole page, thing in if I remove $output = curl_exec($ch); it returns 0 all the time. Does anyone know how to make the performance better?

8 Answers 8

First make sure if the URL is actually valid (a string, not empty, good syntax), this is quick to check server side. For example, doing this first could save a lot of time:

if(!$url || !is_string($url) || ! preg_match('/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:8+)?(\/.*)?$/i', $url))

Make sure you only fetch the headers, not the body content:

@curl_setopt($ch, CURLOPT_HEADER , true); // we want headers @curl_setopt($ch, CURLOPT_NOBODY , true); // we don't need body 

For more details on getting the URL status http code I refer to another post I made (it also helps with following redirects):

$url = 'http://www.example.com'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, true); // we want headers curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT,10); $output = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); echo 'HTTP code: ' . $httpcode; 

Источник

Оцените статью