PHP发布请求,带有file_get_content

时间:2020-07-08 12:05:30

标签: php post token file-get-contents

请有人帮助我

我正在尝试使用POST file_get_contents发出请求,但收到415代码错误消息。我不是专业开发人员,我想知道源代码中有什么问题。这是我的问题:

  1. 我生成一个令牌,该令牌是第二个请求的参数
  2. 生成的令牌必须用于POST请求

我收到415错误代码消息。我不知道如何更正该来源

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

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

$options = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => "Authorization Token " . base64_encode("$token"),
        'content' => $data
    )
);

$context = stream_context_create($options);

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

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

1 个答案:

答案 0 :(得分:0)

如果您只是获得令牌并且仅在邮寄电话中遇到问题,那么我注意到您错过了一些请求标头:

像Content-type,Content-length,我已经检查了不同的URL,对我来说尝试尝试

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

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

    $options = array('http' =>
        array(
            'method'  => 'POST',
            'header' => "Content-type: application/json\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);