Jump to content


Photo

Problem with fgets


  • Please log in to reply
2 replies to this topic

#1 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 21 October 2006 - 02:48 AM

I have a web hosting account with CPanel access.  I'm trying to create a script that will login to cpanel, perform certain actions and then read the output that CPanel creates as a result of my actions.  This script DOES perform the action that it's supposed to do but doesn't receive any of the output.

I've got 2 debugging lines in the code (noted for reference below).  They indicate that Socket does have the resource value but response is empty.

Thanks.

<?php
$cpuser = "myUsername"; // Username used to login to CPanel
$cppass = "myPassword"; // Password used to login to CPanel
$domain = "mydomain.com"; // Domain name where CPanel is run
$skin = "mySkin"; // Set to cPanel skin you use (script won't work if it doesn't match)

// Info required for cPanel access
if ($secure) {
   $url = "ssl://".$domain;
   $port = 2083;
} else {
   $url = $domain;
   $port = 2082;
}
$socket = fsockopen($url,$port);
if (!$socket) { echo "Failed to open socket connection... Bailing out!\n"; exit; }


// Encode authentication string
$authstr = $cpuser.":".$cppass;
$pass = base64_encode($authstr);

// Make POST to cPanel
//The "URL" variable used below DOES get set properly--I've tested and confirmed that.  I just don't want to display that publicly.
fputs($socket,$url . " HTTP/1.0\r\n");
fputs($socket,"Host: $domain\r\n");
fputs($socket,"Authorization: Basic $pass\r\n");
fputs($socket,"Connection: Close\r\n");
fputs($socket,"\r\n");

if($socket){echo $socket . "<br>";}//                 <====== This line shows "Resource id #2"


// Grab response even if we don't do anything with it.
while (!feof($socket)) {
  $response = fgets($socket,4096);
  if ($debug) {echo $response;}
}
if($response){echo "True";}else{echo "False";}//      <====== This line shows False
echo nl2br($response);

fclose($socket);
?>

Don

#2 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 21 October 2006 - 02:22 PM

This is all that gets sent to the browser when I load this script (I did View Source):

Resource id #2<br>False

Don

#3 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 22 October 2006 - 12:29 AM

Ok, I just figured out WHY it wasn't working.  I wasn't appending the response value.  So once the while loop ended the response variable was empty.

Don




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users