2011-06-08 72 views
5

我想通過證書到一個網站,所以我可以使用它的file_get_contents提取一些數據,但它不工作,我得到一個空白頁,所以任何想法這裏有什麼錯?在PHP中傳遞證書cURL幫助

<?php 


$username="[email protected]"; 
$password="Koin"; 

$url="confluence.rogersdigitalmedia.com"; 


$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
$output = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 

$str= file_get_contents("confluence.rogersdigitalmedia.com/display/prodsupport/Team+Calendar"); 
echo $str; 
?> 

這是新的代碼,它仍然沒有工作停留在當我得到的內容登錄屏幕.... screenshot

<?php 
$username="[email protected]"; 
$password="Koin"; 

$url="confluence.rogersdigitalmedia.com"; 


$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 


//Replaced due to special chars in url for username and pass 
//curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
curl_setopt($ch, CURLOPT_USERPWD, urlencode($username) . ':' . urlencode($password)); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
$output = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 

echo file_get_contents('http://confluence.rogersdigitalmedia.com/exportword?pageId=1114407'); 
?> 

新的代碼:我知道$url是URL,對此我必須登錄,但我怎麼樣在$data?我知道這是我的登錄信息,但我怎麼把它(例如,<用戶名>空間<密碼>)?

<?php 
function do_post_request($url, $data, $optional_headers = null) 
{ 
    $params = array('http' => array(
       'method' => 'POST', 
       'content' => $data 
      )); 
    if ($optional_headers !== null) { 
    $params['http']['header'] = $optional_headers; 
    } 
    $ctx = stream_context_create($params); 
    $fp = @fopen($url, 'rb', false, $ctx); 
    if (!$fp) { 
    throw new Exception("Problem with $url, $php_errormsg"); 
    } 
    $response = @stream_get_contents($fp); 
    if ($response === false) { 
    throw new Exception("Problem reading data from $url, $php_errormsg"); 
    } 
    return $response; 
} 
+1

HTTP響應頭文件的外觀如何?你從服務器獲得什麼狀態? – 2011-06-08 14:40:50

回答

4

你可能需要逃避的用戶名@

curl_setopt($ch, CURLOPT_USERPWD, urlencode($username) . ':' . urlencode($password)); 

這是因爲一些字符(如@:)在URL中的特殊含義。您需要轉義它們,以便服務器將它們視爲字符,而不是指示有關HTTP請求的內容。

全部廢棄。你的編輯表明你對HTTP的工作原理有一個基本的誤解。您不能使用CURL將HTTP登錄憑據傳遞給期望填寫Web表單並希望能夠使用file_get_contents(它是完全獨立的API的一部分)登錄的站點。

就您如何實際執行此操作而言......首先,瞭解該網站是否具有API。如果沒有,請確定他們是否真的希望你成爲屏幕抓圖。這將是糟糕的形式屏幕刮一個網站,不允許它。這實際上可能是違反版權法的行爲。

二,編程到API。這意味着以網站執行的方式登錄。這將與表單值(您將需要發送CURLOPT_POSTFIELDS,可能)和cookie(您將需要在每個請求中使用,可能使用CURLOPT_COOKIEFILE)。

瞭解客戶端與服務器之間的關係如何工作,研究API並且不只是在代碼中查找問題並期望它能夠正常工作。

+1

我不能登錄沒有@,因爲除非我的名字後鍵入整個@ ring.gil.com它不會讓我登錄,所以這些都是需要的...我能做什麼?編輯:謝謝我試試看。 – Bulvak 2011-06-08 14:53:48

+0

您需要*轉義*值,以便服務器意識到「@」字符是用戶名的一部分。如果你不編碼,服務器會認爲用戶名是'okh',服務器是'ring.gil.com:Koin @ confluence.rogersdigitalmedia.com'。 – lonesomeday 2011-06-08 14:55:30

+1

現在情況如何?我按照您所說的方式對其進行了編輯(或者至少我認爲如果我錯了,請糾正我)我仍然使用相同的登錄屏幕 – Bulvak 2011-06-08 15:06:25