AnsweredAssumed Answered

requesting the REST-API with PHP

Question asked by mokematt on May 25, 2012
Hi,

I've already installed the Activiti Engine and want to fire requests to the REST-API but I got some problems there.

Here is my little code sniplet:

<?php
function httpRequest($host, $port, $method, $path, $params) {
   // Params are a map from names to values
   $paramStr = "";
   if(isset($params)){
      foreach ($params as $name=>$val) {
         $paramStr .= $name . "=";
         $paramStr .= urlencode($val);
         $paramStr .= "&";
      }
   }

   // Assign defaults to $method and $port, if needed
   if (empty($method)) {
      $method = 'GET';
   }
   $method = strtoupper($method);
   if (empty($port)) {
      $port = 80; // Default HTTP port
   }

   // Create the connection
   $sock = fsockopen($host, $port);
   if ($method == "GET") {
      $path .= "?" . $data;
   }
   fputs($sock, "$method $path HTTP/1.1\r\n");
   fputs($sock, "Host: $host\r\n");
   fputs($sock, "Content-type: application/json\r\n");
   if ($method == "POST") {
      fputs($sock, "Content-length: ".strlen($paramStr)."\r\n");
   }
   fputs($sock, "Connection: close\r\n\r\n");
   if ($method == "POST") {
      fputs($sock, $paramStr);
   }

   // Buffer the result
   $result = "";
   while (!feof($sock)) {
      $result .= fgets($sock,1024);
   }

   fclose($sock);
   return $result;
}

$resp = httpRequest("localhost", 8080, "POST", "activiti-rest/service/login", array("userID" => "kermit", "passwort" => "kermit"));
var_dump($resp);

Here is the result i got:

string(130) "HTTP/1.1 400 Bad Request Server: Apache-Coyote/1.1 Content-Length: 0 Date: Fri, 25 May 2012 13:14:39 GMT Connection: close "

Where is the Problem here?

Outcomes