gpt4 book ai didi

php - 发出 POST 请求

转载 作者:行者123 更新时间:2023-12-04 06:44:13 30 4
gpt4 key购买 nike

我想知道如何像那里描述的那样发出 HTTP POST 请求 http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#UploadingMetadata (创建一个空文档)。
我的代码如下所示:

<?php

$headers = array(
"POST /feeds/default/private/full HTTP/1.1",
"Host: docs.google.com",
"GData-Version: 3.0",
"Content-Length: 287",
"Content-Type: application/atom+xml"
);

$data = "<?xml version='1.0' encoding='UTF-8'?>";
$data .= "<entry xmlns='http://www.w3.org/2005/Atom'>";
$data .= "<category scheme='http://schemas.google.com/g/2005#kind'";
$data .= "term='http://schemas.google.com/docs/2007#document'/>";
$data .= "<title>new document</title>";
$data .= "</entry>";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://google.com/docs/feeds/default/private/full");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);

$result = curl_exec($ch);

print_r($result);

?>

有什么问题吗?我做的请求正确吗?

最佳答案

$data = "<?xml version='1.0' encoding='UTF-8'?>";

用。。。来代替:
$data = "<"."?xml version='1.0' encoding='UTF-8'?".">";

和...
$data .= "term='http://schemas.google.com/docs/2007#document'/>";

和:
$data .= " term='http://schemas.google.com/docs/2007#document'/>";

哦,最后,你不应该是 print_r结果; print_r用于数组和对象,而不是字符串(curl_exec 返回字符串或 null/false),而是使用 var_dump($result);

关于php - 发出 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3898294/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com