Computer Science Canada

PHP cURL Help - PHP to Java sever communication

Author:  jin [ Wed Feb 23, 2011 6:57 pm ]
Post subject:  PHP cURL Help - PHP to Java sever communication

Hi I am trying to communicate between a web server (apache, php) and a chat server (openfire in java) I need to create a chat room from a php call. I found some code that says it does this but i cant get it to work, I have never worked with cURL before so was trying to rewrite this to understand a well but still nothing. index.php and open.php are what i founnd and also showed its output. For the code i wrote i am only trying to login and the index but it keeps displaying the login form its as if the username and password are not being used. Any help would be appreciated.


code:

Index.php

<form name="input" action="open.php" method="post">
  room name:<input type="text" name="roomname"><br>
  room desc:<input type="text" name="roomdesc"><br>
  room topic:<input type="text" name="roomtopic"><br>
  debug<input type="checkbox" name="debug"><br>
  <input type="submit" name="Submit">
</form>



code:

Open.php

<?php
  /*
   * open.php - Sets CURL command line args, with data sent via POST,
   *            then uses PHP system() function to create chat rooms,
   *            using listed options.
   */
 
  $opf_uid = "admin";
  $opf_pwd = "admin";
  $curl_cookie_file = '/tmp/opf_cookie.' . rand() . '.dat';

  if (isset($_POST["roomname"]) and
    isset($_POST["roomdesc"]) and
    isset($_POST["roomtopic"])) {
    if (isset($_POST["debug"])) {
      print "<p>" . $_POST["roomname"] . "</p>\n";
      print "<p>" . $_POST["roomdesc"]  . "</p>\n";
      print "<p>" . $_POST["roomtopic"]  . "</p>\n";
      print "<p>". $curl_cookie_file . "</p>\n";
    }

    $curl_req_1 = "/usr/bin/curl -D " . $curl_cookie_file . " " .
      "-d username=" . $opf_uid . " " .
      "-d password=" . $opf_pwd . " " .
      "-d login=true " .
      "-d url=\"" . urlencode('/muc-room-edit-form.jsp?create=true') ."\" " .
      "\"http://localhost:9090/login.jsp\" &> /dev/null";

     $curl_req_2 = "/usr/bin/curl -b " . $curl_cookie_file . " " .
      "-d roomconfig_persistentroom=true " .
      "-d roomName=" . $_POST["roomname"] . " " .
      "-d roomconfig_roomname=" . $_POST["roomname"] . " " .
      "-d roomconfig_roomdesc=" . urlencode($_POST["roomdesc"]) . " " .
      "-d room_topic=" . urlencode($_POST["roomtopic"]) . " " .
      "-d roomconfig_maxusers=30 " .
      "-d roomconfig_presencebroadcast=true " .
      "-d roomconfig_presencebroadcast2=true " .
      "-d roomconfig_presencebroadcast3=true " .
      "-d roomconfig_roomsecret='' " .
      "-d roomconfig_roomsecret2='' " .
      "-d roomconfig_whois=anyone " .
      "-d roomconfig_publicroom=true " .
      "-d roomconfig_allowinvites=true " .
      "-d roomconfig_enablelogging=true " .
      "-d roomconfig_reservednick=true " .
      "-d Submit=" . urlencode('Save Changes') . " " .
      "\"http://localhost:9090/muc-room-edit-form.jsp?save=true&create=true\" &> /dev/null";

     if (isset($_POST["debug"])) {
      print "<p>". $curl_req_1 . "</p>\n";
      print "<p>". $curl_req_2 . "</p>\n";

    }

    /* You may want to add logic that checks the return value */

    system($curl_req_1);
    system($curl_req_2);
 unlink($curl_cookie_file);
  } 
?>



code:

Output of open.php with debug on

fg

fg

ghj

/tmp/opf_cookie.19581.dat

/usr/bin/curl -D /tmp/opf_cookie.19581.dat -d username=admin -d password=admin -d login=true -d url="%2Fmuc-room-edit-form.jsp%3Fcreate%3Dtrue" "http://localhost:9090/login.jsp" &> /dev/null

/usr/bin/curl -b /tmp/opf_cookie.19581.dat -d roomconfig_persistentroom=true -d roomName=fg -d roomconfig_roomname=fg -d roomconfig_roomdesc=fg -d room_topic=ghj -d roomconfig_maxusers=30 -d roomconfig_presencebroadcast=true -d roomconfig_presencebroadcast2=true -d roomconfig_presencebroadcast3=true -d roomconfig_roomsecret='' -d roomconfig_roomsecret2='' -d roomconfig_whois=anyone -d roomconfig_publicroom=true -d roomconfig_allowinvites=true -d roomconfig_enablelogging=true -d roomconfig_reservednick=true -d Submit=Save+Changes "http://localhost:9090/muc-room-edit-form.jsp?save=true&create=true" &> /dev/null

Warning: unlink(/tmp/opf_cookie.19581.dat) [function.unlink]: No such file or directory in C:\xampp\htdocs\test\open.php on line 72



code:

Mytest.php

<?php

        // Data to post
        $postData = array(
                'username' => 'admin',
                'password' => 'admin',
                'login' => 'true',
        );       
     
        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, "localhost:9090/login.jsp");
       
        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        $output = curl_exec($ch);

        // close curl resource to free up system resources
        curl_close($ch);     
       
        echo $output;
?>


: