2012-01-10 34 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