php到URL(https://www.reliancesmart.in/)中卷曲请求的状态代码始终为0

时间:2017-12-02 11:37:29

标签: php curl

我在PHP中使用curl来检查URL是否正常工作。 还有其他办法吗?

我正在使用此代码

$url = 'https://www.reliancesmart.in/';    
$handle = curl_init($url);
            curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);

            curl_setopt($handle,CURLOPT_USERAGENT,' Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36');

            curl_setopt($handle,CURLOPT_VERBOSE,false);
            curl_setopt($handle,CURLOPT_TIMEOUT,10);
            curl_setopt($handle,CURLOPT_CONNECTTIMEOUT,45);

            curl_setopt($handle,CURLOPT_HEADER,true);
            curl_setopt($handle,CURLOPT_NOBODY,true);

            curl_setopt($handle,CURLOPT_RETURNTRANSFER,true);
            curl_setopt($handle,CURLOPT_FOLLOWLOCATION,true);
            curl_setopt($handle,CURLOPT_MAXREDIRS,5); //follow up to 10 redirections - avoids loops


            curl_setopt($handle,CURLOPT_SSL_VERIFYHOST,0); //fix for certificate issue
            curl_setopt($handle,CURLOPT_SSL_VERIFYPEER,0); 

            $response = curl_exec($handle);
            $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);

            curl_close($handle);
            if($httpCode == 0 || $httpCode == 404 || $httpCode == 500)
                 //URL is working
            else
                 //invalid url
此网址的

$httpCode始终为0。 但是,URL在浏览器中运行良好。

1 个答案:

答案 0 :(得分:1)

$url = 'http://www.reliancesmart.in/Offers/'; 
$headers=get_headers( $url );
$status = ( $headers[0]=='HTTP/1.1 200 OK' ) ? 'OK' : 'NOT OK';

echo '<pre>', $status,PHP_EOL,print_r( $headers,1 ), '</pre>';

这将产生以下内容,您可以解析它以查找http状态代码

OK
Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Date: Sat, 02 Dec 2017 11:48:36 GMT
    [2] => Server: IBM_HTTP_Server
    [3] => X-Frame-Options: SAMEORIGIN
    [4] => Set-Cookie: Apache=10.129.71.6.1512215316899610; path=/; expires=Tue, 30-Nov-27 11:48:36 GMT
    [5] => Pragma: no-cache
    [6] => Cache-Control: no-store, no-cache
    [7] => Expires: now
    [8] => Set-Cookie: JSESSIONID=0000F8X6oOLjDd9zc3_9gcKVwVV:18s01dth5; Path=/
    [9] => Set-Cookie: WC_PERSISTENT=YU5By1U2xfjlFIEIUGRa6jUt6yg%3d%0a%3b2017%2d12%2d02+17%3a18%3a36%2e9%5f1512215316900%2d42281%5f0; Path=/
    [10] => Set-Cookie: WC_SESSION_ESTABLISHED=true; Path=/
    [11] => Set-Cookie: WC_PERSISTENT=v8e48LQNSBlHKMimL4%2fXi0OWdOY%3d%0a%3b2017%2d12%2d02+17%3a18%3a36%2e904%5f1512215316900%2d42281%5f10151%5f%2d1002%2c%2d1%2cINR%5f10151; Path=/
    [12] => Set-Cookie: WC_ACTIVEPOINTER=%2d1%2c10151; Path=/
    [13] => Set-Cookie: WC_USERACTIVITY_-1002=%2d1002%2c10151%2cnull%2cnull%2cnull%2cnull%2cnull%2cnull%2cnull%2cnull%2c5V4nnT1Pveq4KadJdwb5Wef4ajlO1j9r2JYtlLwit9uO5cHrliBEurEkQhdzkppe6Pcfptlu3JWR%0ahnbzBOUU6%2f0FtZ2HkuUlWt6YH6VwMj6hYt%2fiO360UWNB02Xh0dQBLzpXR0lLefM%3d; Path=/
    [14] => Set-Cookie: WC_GENERIC_ACTIVITYDATA=[31322107%3atrue%3afalse%3a0%3ad9nGiGvnDaNy%2fj93mbj7zXdxNPA%3d][com.ibm.commerce.context.audit.AuditContext|1512215316900%2d42281][com.ibm.commerce.store.facade.server.context.StoreGeoCodeContext|null%26null%26null%26null%26null%26null][CTXSETNAME|Store][com.ibm.commerce.context.globalization.GlobalizationContext|%2d1%26INR%26%2d1%26INR][com.ibm.commerce.catalog.businesscontext.CatalogContext|10051%26null%26false%26false%26false][com.ibm.commerce.context.base.BaseContext|10151%26%2d1002%26%2d1002%26%2d1][com.ibm.commerce.context.experiment.ExperimentContext|null][com.ibm.commerce.context.entitlement.EntitlementContext|4000000000000000003%264000000000000000003%26null%26%2d2000%26null%26null%26null][com.ibm.commerce.giftcenter.context.GiftCenterContext|null%26null%26null]; Path=/
    [15] => Set-Cookie: priceMode=1; Path=/
    [16] => X-UA-Compatible: IE=Edge
    [17] => Vary: User-Agent,Accept-Encoding
    [18] => Connection: close
    [19] => Content-Type: text/html;charset=UTF-8
    [20] => Content-Language: en-US
)