Php 发帖
我想知道如何像上面描述的那样发出HTTPPOST请求(创建一个空文档)。 我的代码如下所示:Php 发帖,php,api,curl,Php,Api,Curl,我想知道如何像上面描述的那样发出HTTPPOST请求(创建一个空文档)。 我的代码如下所示: <?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" );
<?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'?>";
与:
哦,最后,你不应该打印结果
print\r
用于数组和对象,而不是字符串(curl\u exec返回字符串或null/false),而是使用var\u dump($result)代码>此外,您的自定义标题看起来很奇怪:
帖子根本不是标题,所以这是完全错误的
主机:是由curl本身添加的,设置它没有意义
内容长度:是由curl本身完成的,如果您弄错了,您可能会混淆curl
$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'/>";