我有一个在 localhost 中有效但在实时网站中无效的 curl。我不知道为什么

Kim*_*cks 1 php curl

我有这个链接

http://www.bata.com.sg,这个网站确实存在

这适用于我的 curl 代码,用于检查页面是否存在。

它适用于我的本地主机代码,但它在我的实时网站中一直失败。

我已经使用其他域(如http://www.yahoo.com.sg )进行了测试,它一直在我的本地主机和我的实时网站上运行。

我复制了这段代码http://w-shadow.com/blog/2007/08/02/how-to-check-if-page-exists-with-curl/逐字逐句。

我不明白为什么这个特定的 url 会失败。

我的网站是由 site5 托管的。

我注意到我一直为这条线获取错误(布尔值)

curl_exec($ch);

我得到这个 curl_error 无法解析主机 'www.bata.com.sg'

请指教。

Ant*_*nna 5

您需要与 site5 的客户支持交谈以找出他们的服务器无法解析 www.bata.com.sg 的原因

在您得到他们的答复之前,请尝试以下代码。

关键点

  1. 它连接到 IP 地址 www.bata.com.sg 解析为 - 194.228.50.32
  2. 然后发送 Host: www.bata.com.sg 标头

本质上,如果它可以解析地址,它的工作方式与 Curl 相同。

<?php

// this is the IP address that www.bata.com.sg resolves to
$server = '194.228.50.32';
$host   = 'www.bata.com.sg';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $server);

/* set the user agent - might help, doesn't hurt */
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);


/* try to follow redirects */
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

/* timeout after the specified number of seconds. assuming that this script runs
on a server, 20 seconds should be plenty of time to verify a valid URL.  */
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);


$headers = array();
$headers[] = "Host: $host";

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

curl_setopt($ch, CURLOPT_VERBOSE, true);

/* don't download the page, just the header (much faster in this case) */
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, true);

$response = curl_exec($ch);
curl_close($ch);

var_dump($response);
Run Code Online (Sandbox Code Playgroud)