用cURL发送用户名和密码

问题描述:

我正在尝试使用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调用。 –

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