Submit XML via Put with CakePHP

I want to send a put request to an API that wants to get the request details in XML

Apparently I need to send xml as a file when using PUT with PHP.

How can i do this?

Here is what I am trying:

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

where $ put is the url and $ fh is the file I made on the fly like this

 $xmlObject = Xml::fromArray($xmlArray); $xmlString = $xmlObject->asXML(); $fh = fopen('php://memory', 'rw'); fwrite($fh, $xmlString); rewind($fh); 
+4
source share
2 answers

I ended up just using php and not php helpers

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

I would prefer to use Cake classes for accuracy, but this works with the api that I used.

0
source

I tested it on cake 2.0.5, and HttpSocket :: put can send arrays of key values ​​or raw strings like postdata.

So you can directly send the xml string and the remote server will read it in Raw Post Data i. e. file_get_contents("php://input")

It works:

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

To demonstrate this, I created a controller called RequestXmlTestController registered under 'Controllers/RequestXmlTestController.php' (code below) and an empty view presented under 'RequestXmlTests/index.ctp'

Controller Code:

 <?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 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'); } } 

References: HttpSocket Documentation

+3
source

Source: https://habr.com/ru/post/1390176/


All Articles