api – 使用CakePHP通过Put发送XML

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了api – 使用CakePHP通过Put发送XML脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想向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);

解决方法

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

因此,您可以直接发送xml字符串,远程服务器将在Raw Post Data i中读取它.即的file_get_contents( “PHP://输入”)

这有效:

$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的Controller,并在’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

脚本宝典总结

以上是脚本宝典为你收集整理的api – 使用CakePHP通过Put发送XML全部内容,希望文章能够帮你解决api – 使用CakePHP通过Put发送XML所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。