栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带JSON正文的POST请求

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带JSON正文的POST请求

您需要使用cURL库发送此请求。

<?php// Your ID and token$blogID = '8070105920543249955';$authToken = 'OAuth 2.0 token here';// The data to send to the API$postData = array(    'kind' => 'blogger#post',    'blog' => array('id' => $blogID),    'title' => 'A new post',    'content' => 'With <b>exciting</b> content...');// Setup cURL$ch = curl_init('https://www.googleapis.com/blogger/v3/blogs/'.$blogID.'/posts/');curl_setopt_array($ch, array(    CURLOPT_POST => TRUE,    CURLOPT_RETURNTRANSFER => TRUE,    CURLOPT_HTTPHEADER => array(        'Authorization: '.$authToken,        'Content-Type: application/json'    ),    CURLOPT_POSTFIELDS => json_enpre($postData)));// Send the request$response = curl_exec($ch);// Check for errorsif($response === FALSE){    die(curl_error($ch));}// Depre the response$responseData = json_depre($response, TRUE);// Print the date from the responseecho $responseData['published'];

如果由于某种原因您不想/不想使用cURL,可以这样做:

<?php// Your ID and token$blogID = '8070105920543249955';$authToken = 'OAuth 2.0 token here';// The data to send to the API$postData = array(    'kind' => 'blogger#post',    'blog' => array('id' => $blogID),    'title' => 'A new post',    'content' => 'With <b>exciting</b> content...');// Create the context for the request$context = stream_context_create(array(    'http' => array(        // http://www.php.net/manual/en/context.http.php        'method' => 'POST',        'header' => "Authorization: {$authToken}rn". "Content-Type: application/jsonrn",        'content' => json_enpre($postData)    )));// Send the request$response = file_get_contents('https://www.googleapis.com/blogger/v3/blogs/'.$blogID.'/posts/', FALSE, $context);// Check for errorsif($response === FALSE){    die('Error');}// Depre the response$responseData = json_depre($response, TRUE);// Print the date from the responseecho $responseData['published'];


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/437094.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号