首先,我是 CURL 新手,但不是 PHP 新手。
我有以下代码。
当 $url 位于本地主机时,这可以正常工作,但当 url 是我的实时服务器时,则失败。我查过与标题类似的问答据我所知,我不在代理服务器后面。我可以使用多种浏览器中的任何一种完美地访问“实时”网址。我有几个“开发”本地主机站点,它们都从curl 返回数据。似乎没有一个等效的“实时”网站(或 google.com、stackoverflow.com)可以访问。所有这些都会产生相同的错误消息。
#Note this is within a public function within a class so $this->currentword has been defined as a string
$url = "http://example.com/{$this->currentword}";
#$url = "http://example.localhost/{$this->currentword}"; // this works returns the content ok
$agent = "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0";
if (function_exists('curl_version')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl, CURLOPT_TIMEOUT, 45);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); …Run Code Online (Sandbox Code Playgroud)