Hey Mike,
 
I just copied that code into a quick script, changed the $username, $password, 
and $cam_hostname, and it worked on version 4.1.1.  At some point they changed 
the way the API worked - from accepting the parameters 
(op,qtype,admin,passwd,etc) in the request portion of the POST to only 
accepting them in the data portion.  It seems as though it was changed back in 
a later version though....as what you pasted worked for me.
 
Anyways, try constructing the header like this:
 
    $cam_admin_username = "admin";
    $cam_admin_password = "password";
    $cam_hostname       = "hostname.school.edu";
    $file="/admin/cisco_api.jsp?";
    $login_by_function="&admin=" . $cam_admin_username . "&passwd=" . 
$cam_admin_password;
    // use the standard format.  file.php?var1=test&var2=anything&var3=1337
    $post = "op=adminlogin";
    $data = $post . $login_by_function;
    // Build the header
    $header = "POST $file HTTP/1.0\r\n";
    $header .= "Host: $cam_hostname\r\n";
    $header .= "Content-type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-length: " . strlen($data) . "\r\n";
    $header .= "Accept: */*\r\n";
    $header .= "\r\n";
    $header .= "$data\r\n";
    $header .= "Connection: close\r\n\r\n";

what you pasted earlier does appears to work fine on cca 4.1.1 for me.  Here's 
the output:
 
<snip>
shellbox# php cca.php
<pre>HTTP/1.1 200 OK
Date: Thu, 13 Dec 2007 08:09:09 GMT
Server: Apache
Set-Cookie: JSESSIONID=642952F******B80549A72B44BF; Path=/admin; Secure
Content-Length: 61
Connection: close
Content-Type: text/html;charset=ISO-8859-1

  <br /><br />error=No logged in user matches the criteria
</snip>
 
Feel free to contact me off list if you're getting stuck somewhere, although 
keep in mind I'm on west coast time now 8-)
Regards,

Alex Lanstein
[EMAIL PROTECTED]

________________________________

From: Cisco Clean Access Users and Administrators on behalf of King, Michael
Sent: Wed 12/12/2007 9:25 AM
To: [email protected]
Subject: API username



Ok, I'm plugging away with the API, teaching myself PHP as I go, but I'm having 
a few problems.  (I'm using Alex Lanstein's example code he posted a few months 
ago)

 

I used this PHP script, and I get error=Invalid operation

If I switch the $post variable with the commented out one, I can get a session 
ID, so the username password is good, and the webserver can reach/use the API.

 

Am I not formatting the string to be passed to the API correctly?  I've even 
tried $post = "op=getuserinfo&qtype=all&qval="; which should work as well.

 

<?

$cam_admin_username = "username";

$cam_admin_password = "password";

##EX: hostname.yourschool.edu

$cam_hostname       = " hostname.yourschool.edu ";

$file="/admin/cisco_api.jsp?";

$login_by_function="&admin=" . $cam_admin_username . "&passwd=" . 
$cam_admin_password;

#// $post = "op=adminlogin";

$post = "op=getuserinfo&qtype=mac&qval=001B77242E52";

$data = $file . $post . $login_by_function;

#// Build the header

$header = "POST $data HTTP/1.1\r\n";

$header .= "Host: $cam_hostname\r\n";

$header .= "Content-type: text/html\r\n";

#$header .= "Content-length: " . (strlen($data)). "\r\n";

$header .= "Connection: close\r\n\r\n";

 

$connection = pfsockopen("ssl://$cam_hostname", 443, $errno, $errstr);

if ($connection)

{

fwrite($connection, $header);

while (!feof($connection)) {$buffer .= fgets($connection,128); }

fclose($connection);

}

else echo "$errno --- $errstr";

 

// clean up the output.  CAM displays output in 'hidden' html comments

 

$buffer = str_replace("<!--", "<br /><br />", $buffer);

$buffer = str_replace("-->", "", $buffer);

echo "<pre>";

print_R($buffer);

 

?> Michael King

Technology Systems & Networking

Bridgewater State College

 

Reply via email to