Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sendRequest problem

Status
Not open for further replies.

Trebor100

Programmer
Mar 14, 2006
90
GB
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
 
You might tell us what's not happening !!!
Are you getting a syntax error ?
Is it running but not setting $xmlstr to the correct value?
What does sendRequest do ?
What version of php was on your laptop?
What version are you using now
Can you echo $path and $body to see what they contain?
 
Sorry ingresman - info as follows:
No syntax error - it just stops dead on that line

My bad on sendRequest - had assumed it was a method. Code for the function is as follows:

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- ."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";
}

I'm using latest version of php (v5.2.something). Not sure what was on the old laptop as that was setup ages ago - was v5 or later though.
 
okay - a few more echos put in and the exact line it dies on is:

$s = socket_create(AF_INET, SOCK_STREAM, 0);
 
problem solved - had sockets commented out in php.ini..... doh!
 
Result !,
check out the error reporting values. PHP can tell you that it can't find functions. Of course revert back to minimum errors after checking your site over.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top