做一个POST请求 [英] Make a POST request

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

问题描述

我想知道如何使一个HTTP POST请求喜欢它的有<一个描述href=\"http://$c$c.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#UploadingMetadata\" rel=\"nofollow\">http://$c$c.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#UploadingMetadata (创建一个空文件)。
我的code是这样的:

 &LT; PHP$头=阵列(
    POST /供稿/默认/私营/全HTTP / 1.1,
    主持人:docs.google.com
    的GData-版本:3.0,
    内容长度:287
    内容类型:应用程序/原子+ XML
);$数据=&LT; XML版本='1.0'编码='UTF-8'&GT;?
$数据=&LT;入境的xmlns =HTTP://www.w3.org/2005/Atom'>。
$数据=&LT;类计划=HTTP://schemas.google.com/g/2005#kind'。
$数据=一词=的http://schemas.google.com/docs/2007#document'/>。
$数据=&LT;标题&gt;新建文件&LT; /标题&gt;中。
$数据=&LT; /进入&gt;中;$ CH = curl_init();    curl_setopt($ CH,CURLOPT_URLhttp://google.com/docs/feeds/default/private/full);
    curl_setopt($ CH,CURLOPT_POST,真正的);
    curl_setopt($ CH,CURLOPT_POSTFIELDS,$数据);
    curl_setopt($ CH,CURLOPT_HTTPHEADER,$头);
    curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
    curl_setopt($ CH,CURLOPT_HEADER,真正的);$结果= curl_exec($ CH);的print_r($结果);?&GT;

有什么不对呢?我在做正确的请求?


解决方案

  $数据=&LT; XML版本='1.0'编码='UTF-8'&GT;? ;

替换为:

  $数据=LT;XML版本='1.0'编码='UTF-8'?。&gt;中;

和...

  $ =数据一词=的http://schemas.google.com/docs/2007#document'/>。

使用:

  $ =数据一词=的http://schemas.google.com/docs/2007#document'/>。


呵呵,最后,你不应该的print_r ING的结果; 的print_r 是数组和对象,而不是字符串(curl_exec返回一个字符串或空/假),而使用的var_dump($结果);

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

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

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