开发者

Make a POST request

I would like to know how to make a HTTP POST request like it's described there http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#UploadingMetadata (Creating an empty document). My code looks like this:

<?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);

?>

What's wrong there? Am I doing request correctly?


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

Replace with:

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

And...

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

With:

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

Oh and finally, you shouldn't be print_ring the result; print_r is for arrays and objects, not strings (curl_exec returns a string or null/false), instead use var_dump($result);


Further, your custom headers look weird:

POST is not a header at all so that's plain wrong.

Host: is added by curl itself, no point in setting that.

Content-Length: is done by curl itself, you mostly risk confusing curl if you get it wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜