发出 POST 请求 [英] Make a POST request

查看:36
本文介绍了发出 POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'?>";

替换为:

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


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

这篇关于发出 POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆