HttpRequest(PHP),如何发布对象

时间:2011-10-16 00:19:38

标签: php http post

代码如下:

<?php
//set up variables
$theData = '<?xml version="1.0"?>
<note>
    <to>php.net</to>
    <from>lymber</from>
    <heading>php http request</heading>
    <body>i love php!</body>
</note>';
$url = 'http://www.example.com/script.php';

//create the httprequest object                
$httpRequest_OBJ = new httpRequest($url, HTTP_METH_POST, $options);
//add the content type
$httpRequest_OBJ->setContentType = 'Content-Type: text/xml';
//add the raw post data
$httpRequest_OBJ->setRawPostData ($theData);
//send the http request
$result = $httpRequest_OBJ->send();
//print out the result
echo "<pre>"; print_r($result); echo "</pre>";
?>

现在httprequest在这里调用脚本:

http://www.example.com/script.php

在那里我想访问该对象,以便我可以操纵数据并将其发回:

$httpRequest_OBJ->setRawPostData ($theData);

但它不起作用。我尝试了$_POST['theData'],但只有在使用$r->**addPostFields**(array('user' => 'mike', 'pass' => 's3c|r3t'));

时才有效

如何访问对象$theData

感谢。

1 个答案:

答案 0 :(得分:2)

我认为你所追求的是php://input

因此,您可以获得您发布的大量XML:

$fp = fopen('php://input','r');
$data = '';
while (!feof($fp)) $data .= fread($fp,1024);
// $data should now contain the XML posted in your example

作为旁注,我认为该行

$httpRequest_OBJ->setContentType = 'Content-Type: text/xml';

......应该只是阅读

$httpRequest_OBJ->setContentType = 'text/xml';