如何使用wp_remote_post从Twilio发送短信? 下面的代码很好用,但是需要使用wp_remote_post
通过WordPress HTTP API完成function send_twilio_text_msg($id, $token, $from, $to, $body)
{
$url = "https://api.twilio.com/2010-04-01/Accounts/".$id."/SMS/Messages";
$data = array (
'From' => $from,
'To' => $to,
'Body' => $body,
);
$post = http_build_query($data);
$x = curl_init($url );
curl_setopt($x, CURLOPT_POST, true);
curl_setopt($x, CURLOPT_RETURNTRANSFER, true);
curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($x, CURLOPT_USERPWD, "$id:$token");
curl_setopt($x, CURLOPT_POSTFIELDS, $post);
$y = curl_exec($x);
curl_close($x);
print_r($y);
}
答案 0 :(得分:1)
要使用wp_remote_post
调用Twilio API,您需要做一些事情:
From
,To
和Body
)Authorization
标头。为此,您需要一个带有一个键Authorization
的数组,该值由以64为基数的值编码,并以冒号连接到Account Sid和Auth Token。$url
是wp_remote_post
的第一个参数,第二个参数是具有body
和headers
属性的关联数组。请参见以下示例:
function send_twilio_text_msg($id, $token, $from, $to, $body)
{
$url = "https://api.twilio.com/2010-04-01/Accounts/".$id."/SMS/Messages";
$data = array(
'From' => $from,
'To' => $to,
'Body' => $body
);
$headers = array(
'Authorization' => 'Basic ' . base64_encode($id . ':' . $token)
);
$result = wp_remote_post($url, array(
'body' => $data,
'headers' => $headers
));
}