2011-04-26 205 views
2
<?php 
error_reporting(-1); 
$config = array 
(
    "siteURL"  => "http://domain.com", 
    "loginCheck"  => "checkuser.php", 
    "userAgent"  => "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16" 
); 

$postFields = "username=user&password=pass&submit= Login "; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $config['siteURL'] . $config['loginCheck']); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_USERAGENT, config['userAgent']); 
curl_setopt($ch, CURLOPT_REFERER, config['siteURL']); 

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

echo $content; 

?> 

我希望這是迴應curl的結果,但不是。我做錯了什麼?cURL不返回任何東西?

+4

嘗試一些調試的東西,例如''var_dump($ content);','curl_setopt($ ch,CURLOPT_HEADER,1);''var_dump(curl_getinfo($ ch));' - 你可能得到一個零長度響應,或者cURL可能遇到一些種類的連接錯誤。 – Dereleased 2011-04-26 22:17:59

+0

您正在設置正確的頁面嗎?這只是我過去犯過的一個愚蠢的錯誤。 – esqew 2011-04-26 22:18:16

+0

始終使用此:error_reporting(E_ALL); ini_set(「display_errors」,1);如果您不希望錯誤消息弄亂HTML結果,請將它們寫入日誌文件。 – SteAp 2011-04-26 22:30:31

回答

4

確實如此,如果你設置了「/」末(固定兩個其他錯誤的拼寫)的完整URL:

error_reporting(-1); 
$config = array 
(
    "siteURL"  => "http://www.apple.com/", 
    "loginCheck"  => "checkuser.php", 
    "userAgent"  => "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16" 
); 

$postFields = "username=user&password=pass&submit= Login "; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $config['siteURL'] . $config['loginCheck']); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_USERAGENT, $config['userAgent']); 
curl_setopt($ch, CURLOPT_REFERER, $config['siteURL']); 

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

echo $content; 
+2

該死的我忘記了/,我的意思是後來得到,但忘了它。現在工作,謝謝。 – Rob 2011-04-26 22:34:48

+0

不客氣!快樂的編碼! – SteAp 2011-04-26 22:39:55

1

的變量中的兩個被命名爲嚴重:

curl_setopt($ch, CURLOPT_USERAGENT, config['userAgent']); 
curl_setopt($ch, CURLOPT_REFERER, config['siteURL']); 

應該是:

curl_setopt($ch, CURLOPT_USERAGENT, $config['userAgent']); 
curl_setopt($ch, CURLOPT_REFERER, $config['siteURL']); 

我的猜測是,PHP故障到一個空白頁(自變量被看作是一個常數,而是常數必須是標量)。

另一個問題可能是用戶代理。如果沒有設置用戶代理,我已經看到完全拒絕回覆的服務器。

+0

不,原來的問題與這個字符串「http://www.apple.com」有關,它應該是「http://www.apple.com/」。 – SteAp 2011-04-26 22:23:43

+0

實際上,在真正的代碼執行之前發現並報告了語法錯誤,所以我報告的問題*必須先解決*。 – Christian 2011-04-26 22:27:53

+0

當然。你是對的。但即使有了這兩個修復程序,代碼也會迴應說明 - 由於URL格式不正確。 – SteAp 2011-04-26 22:31:40

1

您可以使用一個類,我寫了周圍捲曲該套。要安裝,請參閱:https://github.com/homer6/altumo

使用起來更容易,並且可以爲您提供一些輕鬆訪問調試的方法。例如:

try{ 

    //load class autoloader 
     require_once(__DIR__ . '/loader.php'); //you should ensure that is is the correct path for this file 

    //make the response; return the response with the headers 
     $client = new \Altumo\Http\OutgoingHttpRequest('http://www.domain.com/checkuser.php', array(
      'username' => 'user', 
      'password' => 'pass', 
      'submit' => ' Login ' 
     )); 
     $client->setRequestMethod(\Altumo\Http\OutgoingHttpRequest::HTTP_METHOD_POST); 

    //send the request (with optional arguments for debugging)   
     //the first true will return the response headers 
     //the second true will turn on curl info so that it can be retrieved later 
     $response = $client->send(true, true); 

    //output the response and curl info 
     \Altumo\Utils\Debug::dump($response, $client->getCurlInfo()); 

    //alternatively, you can get the response wrapped in an object that allows you to retrieve parts of the response 
     $http_response = $client->sendAndGetResponseMessage(true); 
     $status_code = $http_response->getStatusCode(); 
     $message_body = $http_response->getMessageBody(); 
     $full_http_response = $http_response->getRawHttpResponse(); 
     \Altumo\Utils\Debug::dump($status_code, $message_body, $full_http_response); 


}catch(\Exception $e){ 

    //This will display an error if any exceptions have been thrown 
    echo 'Error: ' . $e->getMessage(); 

} 

希望幫助...