2016-11-07 112 views
0

我正在嘗試使用php cURL訪問API。該文檔說:用cURL發送用戶名和密碼

每個請求必須包含分配給電子郵件 合作伙伴的API密鑰。它應該是

呈現給通過標準基本HTTP認證服務器(如在RFC2617中定義 )作爲

用空密碼的用戶名。

我該如何發送API密鑰作爲用戶名並讓PW空着?以下是我迄今爲止:

$subscriberInfo = [ 
    'email' => $email, 
    'search' => $jobType, 
    'location' => $location 
]; 

$ch = curl_init('https://api.ExternalSiteHere'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $subscriberInfo); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: multipart/form-data' 
    )); 

// execute! 
$response = curl_exec($ch); 

// close the connection 
curl_close($ch); 

var_dump($response); 
die(); 
+0

沒有可能通過GET發送?例如'https://api.ExternalSiteHere?api_key = 123456abcdef' – Dwza

+0

不,不是用這個API調用。 –

回答

2

curl_setopt($ch, CURLOPT_USERNAME, base64_encode("{$username}:{$password}"));

在你的情況

curl_setopt($ch, CURLOPT_USERNAME, base64_encode("{$username}:"));

如果不行名後降結腸

+0

謝謝,這工作。我只需要刪除'base64_encode'。所以最終看起來像這樣:'curl_setopt($ ch,CURLOPT_USERNAME,「username」);' –

+0

奇怪的通常在基本授權中它需要base64_encode;無論如何高興它的工作 – DdD

相關問題