【发布时间】:2012-02-15 06:22:02
【问题描述】:
cURL 在服务器上时不返回任何内容。在本地主机上一切正常,但是当它在远程托管时 getSearchResults() 什么都不返回(或 302 标头)。这是服务器配置有问题吗(尝试了 2 种不同)。可以是 CURLOPT_FOLLOWLOCATION 吗?在 localhost 上尝试了 true 和 false - 仍然有效。在远程托管上,由于某种原因,它不允许跟随位置,但如果它在没有本地的情况下工作,我认为这并不重要。
<?php
class cURL
{
private $username;
private $password;
private static $tmpfname;
public function __construct($username,$password) {
$this->username = $username;
$this->password = $password;
$this->makeCookies($username, $password);
}
private function makeCookies($username, $password) {
self::$tmpfname = tempnam("/tmp", "Cookie");
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_COOKIEFILE, self::$tmpfname);
curl_setopt($ch, CURLOPT_COOKIEJAR, self::$tmpfname);
curl_setopt($ch, CURLOPT_URL,"http://vk.com/login.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "email={$username}&pass={$password}");
ob_start();
curl_exec($ch);
ob_end_clean();
curl_close($ch);
unset($ch);
}
private function getHTML($url){
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_COOKIEFILE, self::$tmpfname);
curl_setopt($ch, CURLOPT_COOKIEJAR, self::$tmpfname);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}
public function getSearchResults($songname) {
$songname = urlencode($songname);
$contents = $this->getHTML("http://vk.com/search?c[section]=audio&c[q]={$songname}");
return $contents;
}
}
?>
【问题讨论】:
-
我从来没有听说过在这样的主机上禁用了特定的 cURL 选项。联系您的主机或交换机。
-
@ceejayoz 不是 curl 选项被禁用,如果 PHP 在安全模式下运行,
FOLLOWLOCATION被禁用。 -
@heroix 友情提醒!请使用受保护而不是私有。它允许以后进行可扩展性和单元测试,被认为是一种很好的做法。
-
@Ranty 我称之为禁用。你真的应该得到一个没有安全模式的主机,你的生活会容易得多。它太没用了,他们在 PHP 5.3 之后就弃用了它。 php.net/manual/en/features.safe-mode.php
标签: php curl localhost http-status-code-302