Jump to content

[SOLVED] sendRequest problem


Rob17

Recommended Posts

Hi all,

 

Im v new to PHP and am having a problem on an opensource element of code.  I know the code works as it was functioning on my old laptop before it went bang but i cannot get it working on my new laptop.  The line of code is as follows:

 

$xmlstr = sendRequest($path, $body);

 

I've no doubt this is something I have setup incorrectly but if anyone could point me in the right direction that would be great.

 

Thanks in advance.

Rob

Link to comment
https://forums.phpfreaks.com/topic/157212-solved-sendrequest-problem/
Share on other sites

SendRequest performs the following:

 

function sendRequest($path, $body, $xml_only=true, $repeat=true) {

global $amee_host,$amee_port,$responses;

if(!isset($_SESSION["authToken"]) && strpos($path,"POST /auth")===false){

    setAuthToken();

}

$authToken=$_SESSION["authToken"];

$header = $path." HTTP/1.0\n"

    .getCookieLines() //insert cookies

    ."Accept: application/xml\n";

if($authToken!=null)

$header.="authToken: ".$authToken."\n";

$header.="Host: ".$amee_host."\n"

    ."Content-Type: application/x-www-form-urlencoded\n"

    ."Content-Length: ".strlen($body)."\n"

    ."\n"

    .$body;

 

  //echo($header."<br/>");

  $s = socket_create(AF_INET, SOCK_STREAM, 0);

  $z = socket_connect($s, gethostbyname($amee_host), $amee_port);

  socket_write ($s, $header, strlen($header));

 

  $lines=array();

  $lines[0]="";

  $i=0;

  $xml_line="";

  while (true) {

    $c = @socket_read($s, 1);

    echo($c);

    if ($c == "\n" || strlen($c)===0) {

      echo($lines[$i]."<br/>\n");

      if(strpos($lines[$i],"Set-Cookie:")!==false)

      storeCookie($lines[$i]);

      else if(strpos($lines[$i],"<?xml")!==false)

      $xml_line=$lines[$i];

      if(strlen($c)===0)

      break;

      $i++;

      $lines[$i] = "";

    }

    else

        $lines[$i] .= $c;

  }

 

  socket_close($s);

 

  if(strpos($lines[0],"401 UNAUTH")!==false){//auth failed, try again, try ONCE at getting new authToken then trying again

if($repeat===true){

debug("<p><b>Authentication failure - get new token and try again.</b></p>");

setAuthToken();

return sendRequest($path, $body, $xml_only, false); //try just one more time!

}

else

debug("Authentication failure on second attempt.");

  }

  if($_SESSION["debugOn"])

$responses[count($responses)]=array("request"=>$header,"response"=>$lines);

  if($xml_only)

    return $xml_line;

  else

    return $lines;

}

 

//This function creates a new user profile and returns the 12 digit UID.

function createNewProfile(){

$path = "POST /profiles";

$body = "profile=true";

 

try {

    $xmlstr = sendRequest($path, $body);

} catch (Exception $e) {

    echo 'Caught exception: ',  $e->getMessage(), "\n";

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.