入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

php 发起 post http请求 ,并传body内容 到另一个php页面

创建时间:2016-09-23 投稿人: 浏览次数:2051

You need to use the cURL library to send this request.

<?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_encode($postData)
));

// Send the request
$response = curl_exec($ch);

// Check for errors
if($response === FALSE){
    die(curl_error($ch));
}

// Decode the response
$responseData = json_decode($response, TRUE);

// Print the date from the response
echo $responseData["published"];

If, for some reason, you can"t/don"t want to use cURL, you can do this:

<?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}
".
            "Content-Type: application/json
",
        "content" => json_encode($postData)
    )
));

// Send the request
$response = file_get_contents("https://www.googleapis.com/blogger/v3/blogs/".$blogID."/posts/", FALSE, $context);

// Check for errors
if($response === FALSE){
    die("Error");
}

// Decode the response
$responseData = json_decode($response, TRUE);

// Print the date from the response
echo $responseData["published"];
shareimprove this answer edited Jun 4 "13 at 14:50
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像