【问题标题】:Twilio Verify API: "Requested URL was not found"Twilio 验证 API:“未找到请求的 URL”
【发布时间】: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"

我不知道我做错了什么?

【问题讨论】:

    标签: php api curl twilio authy


    【解决方案1】:

    好的,我不知道为什么会这样,但我让它工作了,也许其他人可以解释为什么。我将 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;
    
    ?>
    

    ?

    【讨论】:

    • 很高兴您自己解决了这个问题,我只是想跟进。使用 Authy Verify API 验证代码是通过 GET 而不是 POST 完成的。在此处查看带有示例请求的文档:twilio.com/docs/verify/api/…
    猜你喜欢
    • 1970-01-01
    • 2016-11-03
    • 2019-04-25
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    相关资源
    最近更新 更多