【问题标题】:i have a curl that works in localhost but not in live website. i cannot figure out why我有一个 curl 可以在 localhost 中使用,但不能在实时网站中使用。我不知道为什么
【发布时间】:2009-09-24 05:33:45
【问题描述】:

我有这个链接

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/。

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

我的网站由 site5 托管。

我注意到我在这一行中不断得到错误(布尔值)

curl_exec($ch);

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

请指教。

【问题讨论】:

  • curl_exec之后curl_error($ch)的返回值是多少,返回false?
  • 我得到一个无法解析主机 'www.bata.com.sg'

标签: php curl


【解决方案1】:

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

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

关键点

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

本质上,它的工作方式与 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);

【讨论】:

  • 您的代码可以正常工作,这意味着它无法解决问题所在的 url?但我不确定您提供的服务器 IP 是否是 www.bata.com.sg 正确解析的那个?
  • 是的,这是一个 DNS 服务器问题,由于您使用的是 Site5 的服务,因此您自己可能无能为力。是的,它是 www.bata.com.sg 的 IP,您可以通过命令行自行解决:nslookup www.bata.com.sg 或使用任何基于 Web 的 nslookup 服务。
  • 感谢反。真正的原因是bata.com.sg intodns.com/bata.com.sg 的域名服务器给出了指示。
【解决方案2】:

我已经找到原因了。

使用此网站时,有人告诉我bata.com.sg 的名称服务器存在问题

http://www.intodns.com/bata.com.sg

无论如何,上面的答案也很有用。我从他们那里学到了一些东西。

【讨论】:

    【解决方案3】:

    这可能是防火墙问题。有时托管公司可能会限制网络服务器可以咨询的内容。

    您还可以确保 curl 存在于 php_info() 中。我想你没有提到任何错误。

    你也可以试试

    file_get_contents('http://www.yahoo.com.sg');
    

    【讨论】:

    • 首先它的 file_get_contentS 带有一个 S。其次,如果您查看我从中复制的代码,它试图通过获取标题而不是整个文件来尽可能高效。第三,有问题的奇怪行为 URL 不是 www.yahoo.com.sg 它的 www.bata.com.sg 第四,当我在 bata.com.sg 上尝试 file_get_contents 时,我在实时网站上再次出现错误,但在我的本地主机上却没有.
    • 感谢您的努力。我很感激。是的 curl 存在,否则无论输入如何它都不会工作
    • 抱歉,我编辑了我的帖子。我建议使用 file_get_contents 因为它会发出错误消息,同时查看 curl 错误报告将有助于准确地发现发生了什么
    • 你必须检查你的网络服务器的 dns 设置
    猜你喜欢
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多