【发布时间】:2018-05-23 18:43:54
【问题描述】:
我正在使用 curl 执行对 Twilio 验证 API 的请求,遵循此处的说明:https://www.twilio.com/verify/api
使用这些说明,我创建了两个 php 文件来执行 curl 请求 - 一个用于获取验证码 (get_code.php),另一个用于检查验证码 (check_code.php)。这些脚本是使用 ajax post 调用来发送参数的,这两个脚本几乎相同,除了 URL(“/start”与“/check”)。
我相信我指定了正确的 URL,并且 get_code.php 有效,但 check_code.php 会引发以下错误:
未找到请求的 URL。请检查http://docs.authy.com/ 以查看有效的网址。
get_code.php
<?php
$USER_PHONE = htmlspecialchars($_POST["phone"]);
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://api.authy.com/protected/json/phones/verification/start",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'country_code' => '1',
'via' => 'sms',
'phone_number' => $USER_PHONE,
),
CURLOPT_HTTPHEADER => array('X-Authy-API-Key: MY_KEY')
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
check_code.php
<?php
$USER_PHONE = htmlspecialchars($_POST["phone"]);
$VERIFY_CODE = htmlspecialchars($_POST["code"]);
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://api.authy.com/protected/json/phones/verification/check",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'country_code' => '1',
'phone_number' => $USER_PHONE,
'verification_code' => $VERIFY_CODE
),
CURLOPT_HTTPHEADER => array('X-Authy-API-Key: MY_KEY')
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
我使用相同的 URL 和参数在终端中手动执行了 curl,并且成功了。
curl "https://api.authy.com/protected/json/phones/verification/check?phone_number=MY_PHONE&country_code=1&verification_code=MY_CODE" -H "X-Authy-API-Key: MY_KEY"
我不知道我做错了什么?
【问题讨论】: