SendRequest problem :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rob17
Forum Newbie
Posts: 3
Joined: Thu May 07, 2009 7:02 am

SendRequest problem :(

Post by Rob17 »

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
Rob17
Forum Newbie
Posts: 3
Joined: Thu May 07, 2009 7:02 am

Re: SendRequest problem :(

Post by Rob17 »

sendRequest performs the following:

Code: Select all

 
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";
}
 
Last edited by Benjamin on Thu May 07, 2009 11:39 am, edited 1 time in total.
Reason: Added [code=php] tags.
Rob17
Forum Newbie
Posts: 3
Joined: Thu May 07, 2009 7:02 am

Re: SendRequest problem :(

Post by Rob17 »

further to this the exact line it fails on is:

Code: Select all

$s = socket_create(AF_INET, SOCK_STREAM, 0);
Problem now solved - had sockets disabled in php.ini
Last edited by Benjamin on Thu May 07, 2009 11:39 am, edited 1 time in total.
Reason: Added [code=php] tags.
Post Reply