2012-01-10 48 views
4

我想发出一个PUT请求到想要的请求作为XML通过认沽发送XML使用CakePHP

显然,我需要使用PUT用PHP当发送XML作为文件的详细信息的API。

我该怎么做?

这里是我尝试:

$HttpSocket = new HttpSocket(); 
$result = $HttpSocket->put($put, $fh); 

其中$说就是URL和$ FH是我在飞行了这样

$xmlObject = Xml::fromArray($xmlArray); 
$xmlString = $xmlObject->asXML(); 

$fh = fopen('php://memory', 'rw'); 
fwrite($fh, $xmlString); 
rewind($fh); 

回答

0

最后我做这只是使用PHP,而非PHP助手

# write data into a temporary file 
$putData = "<subscription><productPath>$new_product_path</productPath></subscription>"; 
$putDataFile = tmpfile(); 
fwrite($putDataFile, "<subscription><productPath>$new_product_path</productPath></subscription>"); 
fseek($putDataFile, 0); 

# initialize PUT call 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://api.example.com"); 
curl_setopt($ch, CURLOPT_PUT, true); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml")); 
curl_setopt($ch, CURLOPT_INFILE, $putDataFile); 
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putData)); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

# executes PUT call and clean up 
$result = curl_exec($ch); 
$info = curl_getinfo($ch); 
fclose($putDataFile); 
curl_close($ch); 

我用蛋糕类整洁更喜欢,但这部作品与我使用的API。

3

文件我测试过它在Cake 2.0.5上,而HttpSocket :: put可以将键值数组或原始字符串作为postdata发送。

因此,您可以直接发送xml字符串,远程服务器将在Raw Post Data i中读取它。即file_get_contents("php://input")

这工作:

$http = new HttpSocket(); 
$xml_data = Xml::fromArray($data); 
$xml_string = $xml_data->asXML(); 
$response = $http->put('http://example.com', $xml_string); 

为了演示,我创建了一个名为RequestXmlTestController 'Controllers/RequestXmlTestController.php'下一篇(代码如下)控制器,并在'RequestXmlTests/index.ctp'

控制器代码提交一个空的观点:

<?php 
App::uses('AppController', 'Controller'); 
/** 
* RequestXmlTest Controller 
* 
*/ 
class RequestXmlTestController extends AppController { 
/** 
* Use no Model 
*/ 
    public $uses = array(); 

/** 
* index action 
*/ 
    public function index(){ 
    App::uses('HttpSocket', 'Network/Http'); 
    App::uses('Xml', 'Utility'); 
    $http = new HttpSocket(); 

    $data = array(
     'type' => array('name' => 'Campaign', 'data' => array(
     array('name' => 'Come eat at Joe\'s', 'products' => array('adserver', 'analytics')) 
    )) 
    ); 
    $xml_data = Xml::fromArray($data); 
    $xml_string = $xml_data->asXML(); 
    $response = $http->put(Router::url(array('action' => 'test'), true), $xml_string); 
    debug($response); 
    } 

/** 
* test action 
* Test the requests and dump Raw Post Data and Cake's Request object 
*/ 
    public function test(){ 
    var_dump(array('raw_post_data' => file_get_contents("php://input"))); 
    echo "\n\n"; 
    var_dump($this->request); 
    exit; 
    $this->render('index'); 
    } 

} 

参考文献: HttpSocket Documentation

+0

感谢Lourenzo,将它作为字符串发送时,发送给外部服务时不起作用(不是用php/cake或在我的控制下写的) - 技术支持人员说我必须发送文件,而不是字符串。 – Will 2012-01-20 04:03:31

+0

@我想你需要知道哪种数据格式是预期的,编码等等。 我曾经使用过一次.NET SOAP WebService,希望将文件当作ByteArray,并且花费了更多时间,我想把它在一起。 – 2012-01-26 10:59:00