将用户服务请求发送到openfire

时间:2015-02-02 20:31:37

标签: php codeigniter curl openfire

我正在使用codeigniter,我想向openfire发送添加请求,但我真的不明白我必须做什么。我是初学者,我读了这篇指南User Service,我试图添加一个curl Curl lib的新用户,但我被卡住了。 Openfire配置正确,因为如果我从浏览器发出请求,则添加有效 这是我的代码

  $this->load->library('curl');
    $username='usertest';
    $password='password';
    $header='Authorization';
    $content='mykey';
    $this->curl->create('http://myserver.net', 9090);
    $this->curl->http_header($header, $content);
    $this->curl->http_header('Content-Type', 'application/xml');
    $this->curl->post(array('username'=>$username,'password'=>$password)); 
    $this->curl->execute();

但未添加用户

我很抱歉我的英语不好

1 个答案:

答案 0 :(得分:1)

以下是用户服务客户端的一个实现(它基于Curl但不基于codeigniter):

 $url = "http://localhost:9090/plugins/userService/users";
  $xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <user>
     <username>'.$user['username'].'</username>
     <password>'.$user['password'].'</password>
     <name>'.$user['name'].'</name>
     <email>'.$user['email'].'</email>
  </user>';



  //open connection
  $ch = curl_init();


  //set the url, number of POST vars, POST data
  curl_setopt($ch,CURLOPT_URL, $url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  curl_setopt($ch,CURLOPT_HEADER,true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     'Authorization: secret',
     'Content-Type: application/xml'
  ));
  curl_setopt($ch, CURLOPT_POST,           1 );
  curl_setopt($ch, CURLOPT_POSTFIELDS,     $xml );
  #curl_setopt($ch,CURLOPT_POSTFIELDSPOST, count($fields));
  #curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);


  //execute post
  $result = curl_exec($ch);
  $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  ob_start(); // outer buffer
  #echo "Code: $http_status | Username: {$user['name']}\n";
  ob_end_flush();
  //close connection
  curl_close($ch);

  if ($http_status == 201) {
  return true;
  }
  else return false;

您应该在标题中将内容类型和授权设置为数组。 而你的XML内容看起来不对。它不仅是数组中的用户名/密码。