使用 CakePHP 通过 Put 发送 XML

发布于 2024-12-26 01:28:57 字数 454 浏览 4 评论 0原文

我想向一个 API 发送一个 put 请求,该 API 想要将请求的详细信息作为 XML

显然,在将 PUT 与 PHP 结合使用时,我需要将 xml 作为文件发送。

我该怎么做?

这就是我正在尝试的:

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

其中 $put 是 url,$fh 是我像这样动态创建的文件

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

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

I want to send a put request to an API which wants the details of the request as XML

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

How can I do this?

Here is what I'm trying:

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

where $put is the url and $fh is a file i have made on the fly like this

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来世叙缘 2025-01-02 01:28:57

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

因此,您可以直接发送 xml 字符串,远程服务器将在原始帖子数据中读取它,即 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);

为了演示它,我创建了在 'Controllers/RequestXmlTestController.php' 下归档一个名为 RequestXmlTestController 的控制器(代码如下),在 '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 文档

I've tested it on cake 2.0.5, and HttpSocket::put can send key-value arrays or raw strings as postdata.

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

This works:

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

To demonstrate it, I've created a Controller called RequestXmlTestController filed under 'Controllers/RequestXmlTestController.php' (code is below), and an empty view filed 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'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');
  }

}

References:
HttpSocket Documentation

獨角戲 2025-01-02 01:28:57

我最终只使用 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);

为了整洁,我更喜欢使用 Cake 类,但这适用于我正在使用的 api。

I ended up doing this just using php rather than the 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'd have preferred using the Cake classes for neatness, but this works with the api i was using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文