Twilio验证API:“未找到请求的网址”

时间:2018-05-23 18:43:55

标签: php api curl twilio authy

我正在使用curl按照此处的说明执行对Twilio验证API的请求:https://www.twilio.com/verify/api

使用这些说明,我创建了两个php文件来执行curl请求 - 一个用于获取验证码( get_code.php ),另一个用于检查验证码( check_code.php )。使用ajax post调用这些脚本来发送参数,两个脚本几乎相同,除了URL(“/ start”与“/ check”)。

我相信我指的是正确的网址, get_code.php 可以使用,但是 check_code.php 会引发以下错误:

  

找不到请求的网址。请检查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 "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"

我不知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

好吧,好吧,我不知道为什么会这样,但我让它运转起来,也许其他人可以解释原因。我将CURL URL构建为字符串,然后删除了CURLOPT_RETURNTRANSFERCURLOPT_POST个参数。

<?php

$USER_COUNTRY = "1";
$USER_PHONE = htmlspecialchars($_POST["phone"]);
$VERIFY_CODE = htmlspecialchars($_POST["code"]);

$URL = "https://api.authy.com/protected/json/phones/verification/check?country_code=1&phone_number=".$USER_PHONE."&verification_code=".$VERIFY_CODE;

$ch = curl_init();
$curlConfig = array(
    CURLOPT_URL            => $URL,
    CURLOPT_HTTPHEADER => array('X-Authy-API-Key: MY_KEY')
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);

echo $result;

?>

相关问题