2012-07-05 23 views
0

可能重複:
How do I make a request using HTTP basic authentication with PHP curl?HTTP基本授權使用PHP類

我使用的是PHP捲曲類從http://query7.com/php-curl,需要從基本身份驗證一個THRID第三方服務提供商獲取內容。

我不知道如何將我的驗證細節(即用戶名和密碼)傳遞給上述指定的捲髮類。

+0

您可以在URL中傳遞的基本身份驗證憑據。例如http:// user:[email protected]/ – yoavmatchulsky

回答

0

一般來說,你可以將用戶名和密碼傳遞給這樣的域名:

http://username:[email protected]/php-curl 
+0

哇,雖然我需要更改我的網址並將其指向實際的服務網址,但這個作品完美無缺 – Prakash

2

最後一行添加到您的選擇陣列:

public $options = array(
    CURLOPT_RETURNTRANSFER => true,  // return the web page 
    CURLOPT_HEADER   => false, // don't return the headers 
    CURLOPT_FOLLOWLOCATION => true,  // follow redirects 
    CURLOPT_USERAGENT  => "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)", // set a normal looking useragent 
    CURLOPT_AUTOREFERER => true,  // set referer on redirect 
    CURLOPT_CONNECTTIMEOUT => 120,  // timeout 
    CURLOPT_TIMEOUT  => 120,  // timeout 
    CURLOPT_MAXREDIRS  => 10,  // stop after 10 redirects 
    CURLOPT_USERPWD  => $username . ":" . $password // Basic auth 
); 
+0

是的,但是如何將該curl選項傳遞到指定的php類。我看到這個類正在使用一個名爲'$ options'的公共變量。 – Prakash

+0

查看我的更新回答 –

+0

問題在於我無法將您直接向類建議的curl選項添加,這是因爲我使用相同的類用於其他目的(即,獲取不需要認證的頁面) – Prakash