13

StackOverflow に関する最初の質問をすることに非常に興奮しています。私は何年にもわたってかなり多くのことを自分自身に教えるためにそれに頼ってきました!

私の質問はこれです。Mandrill の API を介してメールを送信しようとすると、次のエラーが発生します。

{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"}

次のコードは、メールを送信するために使用しているものです。

<?php
$to = 'their@email.com';
$content = '<p>this is the emails html <a href="www.google.co.uk">content</a></p>';
$subject = 'this is the subject';
$from = 'my@email.com';

$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
$content_text = strip_tags($content);

$postString = '{
"key": "RR_3yTMxxxxxxxx_Pa7gQ",
"message": { 
 "html": "' . $content . '",
 "text": "' . $content_text . '",
 "subject": "' . $subject . '",
 "from_email": "' . $from . '",
 "from_name": "' . $from . '",
 "to": [
 {
 "email": "' . $to . '",
 "name": "' . $to . '"
 }
 ],
 "track_opens": true,
 "track_clicks": true,
 "auto_text": true,
 "url_strip_qs": true,
 "preserve_recipients": true
},
"async": false
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$result = curl_exec($ch);
echo $result;

?>

メッセージ内の検証エラーの原因は何ですか。私は自分の API キーを提供していますが、それは有効です!

誰かが助けてくれることを願っています。ここで一般的に素晴らしいものに感謝します!

ありがとう!

4

7 に答える 7

16

配列だけを使用して、PHP に JSON エンコーディングを処理させたい場合もあります。この特定のエラーは、JSON が何らかの理由で無効な場合によく発生します。したがって、たとえば、次のようにパラメーターを設定できます。

$params = array(
    "key" => "keyhere",
    "message" => array(
        "html" => $content,
        "text" => $content_text,
        "to" => array(
            array("name" => $to, "email" => $to)
        ),
        "from_email" => $from,
        "from_name" => $from,
        "subject" => $subject,
        "track_opens" => true,
        "track_clicks" => true
    ),
    "async" => false
);

$postString = json_encode($params);

json_decode必要に応じて、応答を解析するために使用することもできます。

于 2013-07-17T13:55:12.823 に答える
12

Bansi の回答は Dan B に対して有効でしたが、他の誰かが同じ問題を抱えている場合は、コンテンツに特殊文字 (アクセント、ウムラウト、セディーユ、アポストロフィなど) が含まれているかどうかを確認することをお勧めします。その場合、解決策はテキストを utf8 エンコードすることです。

$content = utf8_encode('<p>Greetings from Bogotá, Colombia. Att:François</p>');
于 2013-12-01T00:14:36.230 に答える
3

マンドリルについてはわかりませんが、$content文字列には二重引用符が含ま"れており、区切り文字$postStringも二重引用符です。これはどの言語でも壊れます。$contentmandril で要求されるように、二重引用符をエスケープする必要があります。

"html": "' . $content . '",に変換されます

"html": "<p>this is the emails html <a href="www.google.co.uk">content</a></p>",
                                            ^                ^

試す

 "html": "' . str_replace('"','\\"',$content) . '",
 "text": "' . str_replace('"','\\"',$content_text) . '",

それ以外の

 "html": "' . $content . '",
 "text": "' . $content_text . '",
于 2013-07-17T12:46:30.550 に答える
0

Dan の curl セットアップを使って HTML で強化されたメッセージを Mandrill に投稿する実験を行ってきましたが、今回は message/send-template.json API の template_content: [] 配列で html を使用しています。

私が気付いたのは、このセットアップ (Bansi による修正を含む) が Mandrill の試行ページで機能しているように見えることでした: https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template

しかし、私のphpスクリプトでは、この頑固なYou must specify a key valueエラーを受け取り続けました. どうやらこのスレッドのおかげで、リクエスト ヘッダーに文字セットとして utf8 を追加して問題を解決しました。

$ch = curl_init();
$headers = array("Content-type: application/json;charset=\"utf-8\""); 
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);

$result = curl_exec($ch);
于 2015-10-20T15:07:28.250 に答える