2012-01-20 5 views
0

cURL은 서버에있을 때 아무 것도 반환하지 않습니다. 모든 것이 localhost에서 잘 작동하지만 원격 호스트에있을 때 getSearchResults()는 아무것도 반환하지 않습니다 (또는 302 헤더). 서버 구성에 문제가 있습니까 (2 가지 시도). CURLOPT_FOLLOWLOCATION이 (가) 뭔가있을 수 있습니까? 로컬 호스트에서 true와 false 모두 ​​시도 - 여전히 작동합니다. 원격 호스팅에서 어떤 이유로 든 위치를 추적 할 수는 없지만, 지역에없는 경우에는 문제가 없다고 생각합니다.cURL은 아무 것도 반환하지 않습니다.

<?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; 
    } 
} 
?> 
+0

이와 같은 호스팅에서 특정 cURL 옵션이 비활성화 된 것을 들어 본 적이 없습니다. 호스트 또는 스위치에 문의하십시오. – ceejayoz

+0

@ceejayoz curl 옵션이 비활성화되어 있지 않습니다. PHP가 안전 모드로 실행되면'FOLLOWLOCATION'이 비활성화됩니다. – Ranty

+1

@heroix 친절한 알림! 개인 대신에 보호 된 것을 사용하십시오. 나중에 확장 성 및 단위 테스트를 허용하며 이는 우수 사례로 간주됩니다. –

답변

1

302 코드는 리디렉션, 그래서 당신은 그것에서 유용한 아무것도 얻을 CURLOPT_FOLLOWLOCATION을 사용할 수 있도록해야합니다.

+0

OP는 그가 이것을 true와 false로 설정했지만 아무런 도움이되지 않는다고 말했습니다 ... –

+2

또한,'ob_start()'와' ob_end_clean()'을 사용하여 cURL의 결과를 캡처하려면'CURLOPT_RETURNTRANSFER' 옵션을 설정하십시오. 'curl_exec()'는 결과를 출력하는 대신 문자열로 반환합니다. 출력을 억제하려면 'CURLOPT_MUTE'를 사용하십시오. –

+0

@GigaWatt : 좋은 추천, 답변으로 넣어 그래서 소음에서 길을 잃지 않아야합니다. –

0

안전 모드에서 php를 실행하는 웹 서버의 경우 웹에서 리디렉션 메커니즘의 구현이 많이 있습니다. 예를 들어, here (실제로 찾아야 할 첫 번째 장소)은 내가 언젠가 내 자신의 스크립트로 수정 한 것입니다. 여러 개의 리디렉션을 처리 할 수 ​​있으며이를 쉽게 이해하고 수정할 수있는 방식으로 작성됩니다.

관련 문제