Jump to content


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


Problem with fgets

Recommended Posts

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.


$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");

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);


Share this post

Link to post
Share on other sites
This is all that gets sent to the browser when I load this script (I did View Source):

[code]Resource id #2<br>False[/code]

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites


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.