RESTFul API POST请求

时间:2020-07-10 10:32:40

标签: php api rest request

我想使用bulksmsonline.com的Restful API发送短信,但是我的代码没什么问题。 凭据是真的,您想进行测试。

https://bulksmsonline.com/developers/

“有趣的API”部分

这是我的代码:

    $tok=file_get_contents('https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/ngosda367/password/Tempcred01');
    $toke =json_decode($tok, true);
    $token=$toke['token'];

    $data = json_encode(
        array(
            'from' => 'TEST',
            'to' => '332698741113',
            'type'=> 'Text',
            'content'=> 'Test',
            'sendDateTime'=> '2020/07/07'
            ))
    ;

    $options = array('http' =>
        array(
            'method'  => 'POST',
            'header' => "Content-type: application/json; charset=utf-8\r\n" .
                        "Content-length: " . strlen($data) . "\r\n" .
                        "Authorization Token: " . base64_encode("$token") . "\r\n",
            'content' => $data
        )
    );

    $context = stream_context_create($options);

    $url = 'https://restapi.bulksmsonline.com/rest/api/v1/sms/send';

    $result = file_get_contents($url,false,$context);
    var_dump($result);

这是我得到的答复:

 failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Reques. bool(false)

THKS为您提供帮助

1 个答案:

答案 0 :(得分:0)

尝试使用get_last_error()(https://www.php.net/manual/en/function.error-get-last.php),然后看看情况如何。或执行curl请求,并使用curl_error()(https://www.php.net/manual/en/function.curl-error.php)查看返回的错误。