gpt4 book ai didi

php - MailGun 401 响应禁止我哪里出错了

转载 作者:行者123 更新时间:2023-12-03 23:16:17 25 4
gpt4 key购买 nike

我有 2 个用于 mailgun api 的脚本,我写的一个返回 401 禁止,我发现在线工作完美的我尝试使用相同的域/api key
这个作品从 github gist 复制粘贴

这个就像一个魅力

<?php
function send_mailgun($email, $body) {
$domain = "SendingDomain";
$config = array();
$config['api_key'] = "key-mailgunkey";
$config['api_url'] = "https://api.mailgun.net/v3/" . $domain . "/messages";
$message = array();
$message['from'] = "Mailgun <user@SendingDomain>";
$message['to'] = $email;
$message['h:Reply-To'] = "<user@SendingDomain>";
$message['subject'] = "Eye-Catching Subject Line";
$message['html'] = $body;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $config['api_url']);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:{$config['api_key']}");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $message);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
echo send_mailgun("personal@domain.com", "Body of the message here!");
?>

而这个不行
<?php
$to = "<personal@domain.com>";
#$bcc = null;
$replyTo = "<dinner@sendingdoamin>";
#$tag = "tag";
$subject = "subject";
#$text = "HI Test \n\r.";
$html ="<h1>hi</h1>";
$fromUser ='hi <user@';
$domain = 'domain';

#$filePath=!empty($img)?curl_file_create($img):null;
$dataSubmit=array(
'from' => $fromUser.$domain.'>',
'to' => $to,
#'bcc' => $bcc,
#'o:tag' => $tag,
#'text' => $text,
'h:Reply-To' => $replyTo,
'subject' => $subject,
'html' => $html,
#'inline[1]' => $filePath,
#'attachment[1]' => $filePath
);
echo '<pre>';
print_r($dataSubmit);
$service_url = 'https://api.mailgun.net/v3/'.$domain.'/messages';
var_dump($service_url);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, 'key-mailgunkey');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 45);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataSubmit);
$result = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
var_dump($result);
var_dump($info);


?>

正如你所看到的,我注释掉了我发现的差异,以确保它们具有相同的 curl 选项,一个有效,另一个无效。

最佳答案

你忘记的这些小事会让你发疯。

curl_setopt($curl, CURLOPT_USERPWD, 'key-mailgunkey'); 

应该以“api:”为前缀,如下所示
curl_setopt($curl, CURLOPT_USERPWD, 'api:key-mailgunkey'); 

这就是让 Camel 折回的差异

关于php - MailGun 401 响应禁止我哪里出错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50166781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com