使用自定义标头发送POST请求

时间:2015-12-11 10:49:25

标签: php blogger

我正在尝试实施博客v3 api to create a new post

这是我的PHP代码,它应该将请求发布到服务器,但它会返回错误

  

警告:   的file_get_contents(https://www.googleapis.com/blogger/v3/blogs/3333333333333/posts/):   无法打开流:HTTP请求失败! HTTP / 1.0 401未经授权   在第24行的/var/www/html/playscript.prijm.com/TempFiles/1449828856x.php中   布尔(假)

以下是代码:

<?php
    $blog_id = 'dddddddddddddd';
    $api_key = 'AIzaccccccccccKUCKo';

    $url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blog_id.'/posts/';
    $data = '{
      "kind": "blogger#post",
      "blog": {
        "id": "'.$blog_id.'"
      },
      "title": "A new post",
      "content": "With <b>exciting</b> content..."
    }';

    $options = array(
        'http' => array(
            'header'  => "Authorization: ".$api_key."\r\nContent-type: application/json\r\n",
            'method'  => 'POST',
            'content' => $data,
        ),
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    var_dump($result);

0 个答案:

没有答案