没有从api post获得响应

时间:2019-05-04 07:27:36

标签: php api codeigniter file-get-contents

我正在开发API,它提供了有关邮递员的数据,但是当我编写代码时,它却无法打开流:

  

HTTP请求失败! HTTP / 1.1 401未经授权。

感谢任何人都可以帮助

我在标头中具有传递值,并且在文件中按内容获取正文中的传递参数

        $postdata = http_build_query(
        array(
           'phone' => '1111111111',
           'city' => 'Kota',
           'name' => 'ABC',
           'pin' => '324005',
           'address' => 'address',
           'country' => 'India',
           'contact_person' => 'name',
           'email' => 'a@gmail.com',
           'registered_name' => 'registered_name',
           'return_address' => 'return_address',
           'return_pin' => '110017',
           'return_city' => 'delhi',
           'return_state' => 'delhi',
           'return_country' => 'india',
       )
    );

    $opts = array('https'=> 
        array (
            'method' => 'POST',
               'header'=>array(
                    'authorization: Token', // removed token for protection
                    'Content-type: application/xwww-form-urlencoded',
                    'content-type: application/json',
               ),

               'content-Type' =>$postdata,
        )
    );

    $url="https://staging-express.delhivery.com/api/backend/clientwarehouse/create/";
    $context=stream_context_create($opts);
    $result=file_get_contents($url,false,$context);

1 个答案:

答案 0 :(得分:0)

我认为这是无效的:

$opts = array('https'=> 

即使对于https请求,它也应该是:

$opts = array('http'=> 

此外,如果我没看错,我想你是

'content-Type' =>$postdata,

应为:

'content' =>$postdata,