【问题标题】:Send userservice request to openfire向 openfire 发送用户服务请求
【发布时间】:2015-04-01 21:33:53
【问题描述】:

我正在使用 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();

但是没有添加用户

对不起,我的英语不好

【问题讨论】:

  • 谢谢,在本指南的帮助下没有更多错误,但是没有添加用户,我认为是autentication或post参数有问题

标签: php codeigniter curl openfire


【解决方案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 内容看起来不对。它不仅是数组中的用户名/密码。

【讨论】:

    猜你喜欢
    • 2014-04-19
    • 1970-01-01
    • 2012-04-07
    • 2016-11-21
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    相关资源
    最近更新 更多