带JSON正文的POST请求

带JSON正文的POST请求,第1张

带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'];


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5128733.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存