文件我測試過它在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
感謝Lourenzo,將它作爲字符串發送時,發送給外部服務時不起作用(不是用php/cake或在我的控制下寫的) - 技術支持人員說我必須發送文件,而不是字符串。 – Will 2012-01-20 04:03:31
@我想你需要知道哪種數據格式是預期的,編碼等等。 我曾經使用過一次.NET SOAP WebService,希望將文件當作ByteArray,並且花費了更多時間,我想把它在一起。 – 2012-01-26 10:59:00