Page 1 of 1

PHP Socket response

Posted: Thu Sep 03, 2009 11:22 am
by danturn
Hey guys...

I've got a working bit of code that pushes XML code to a web server (cisco phone)

It returns a variable called $response.... BUT it doesn't contain the info i actually want back from the phone!

Basically when i send the XML to the phone i should get back this as the $response variable:

<?xml version="1.0" encoding="iso-8859-1"?>
<CiscoIPPhoneResponse>
<ResponseItem URL="Key:Speaker" Data="Success" Status="0" />
</CiscoIPPhoneResponse>

instead i get back this:

HTTP/1.1 200 OK
Content-Type: text/xml; charset="utf-8"
Date: Thu, 03 Sep 2009 16:13:57 GMT
Expires: Thu, 26 Oct 1995 00:00:00 GMT
Last-Modified: Thu, 03 Sep 2009 16:13:57 GMT
Pragma: no-cache
Content-Length: 156
Server: Allegro-Software-RomPager/4.34

The phone is sending back both packets BUT its putting the wrong one into the $response variable... the correct one is the 2nd bit that the phone sends so I dont know if its just putting the first thing it receives into the variable...

any ideas how i can get it to return the correct response?

Code here:

Code: Select all

    
$xml = "XML= <CiscoIPPhoneExecute><ExecuteItem Priority='0' URL='Key:".$key."'/></CiscoIPPhoneExecute>";
    $posttxt = "/CGI/Execute";
    sendXML($xml,$posttxt)
 
function sendXML($xml,$posttxt)
    {
        $ip = $_POST['IP'];
        $userid = $_POST['USERID'];
        $pin = $_POST['PIN'];
        $auth = base64_encode($userid.":".$pin);
        $xml = "XML=".urlencode($xml);
        $post ="POST $posttxt HTTP/1.0\r\n";
        $post.="Host: $ip\r\n";
        $post.="Authorization: Basic $auth\r\n";
        $post.="Connection: close\r\n";
        $post.="Content-Type: application/x-www-form-urlencoded\r\n";
        $post.="Content-Length: ".strlen($xml)."\r\n\r\n";
        $response = "";
        $sock = fsockopen($ip, 80, $errno, $errstr, 30);
        if ($sock) {
            fwrite($sock, $post . $xml);
        while (!feof($sock)) {
            $response .= fgets($sock, 128);
        }
        fclose($sock);
        }
        
echo $response;

Re: PHP Socket response

Posted: Thu Sep 03, 2009 11:26 am
by John Cartwright
What does the built request headers look like?

Re: PHP Socket response

Posted: Thu Sep 03, 2009 6:07 pm
by Darhazer
$response contains both header and body, so you have to split them

Code: Select all

list($header, $body) = explode("\r\n\r\n", $response);
echo $body;
Keep in mind that if you do this in a browser, it won't display anything because it is enclosed in tags... you have to view source to see the XML.

Re: PHP Socket response

Posted: Fri Sep 04, 2009 3:38 am
by danturn
Hey guys thanks for the info...

John,

I'm a bit of a n00b when it comes to this sort of thing... i dont know what you mean..

this is the whole request i believe...

Code: Select all

       $post ="POST $posttxt HTTP/1.0\r\n";
        $post.="Host: $ip\r\n";
        $post.="Authorization: Basic $auth\r\n";
        $post.="Connection: close\r\n";
        $post.="Content-Type: application/x-www-form-urlencoded\r\n";
        $post.="Content-Length: ".strlen($xml)."\r\n\r\n";
do i need a header bit in there?

Darhazer,

I tried that, it looks like the body is empty... if i echo $body, $header and $response i just see the same thing twice (i checked the source and cant see the XML either :*( )

Re: PHP Socket response

Posted: Fri Sep 04, 2009 10:52 am
by Darhazer
Anyway, would you please send the output of the following:
echo strlen($response);

Re: PHP Socket response

Posted: Sat Sep 05, 2009 8:15 am
by danturn
hey Darhazer,

When i do that it returns "418"..

$response is "HTTP/1.1 200 OK Content-Type: text/xml; charset="utf-8" Date: Sat, 05 Sep 2009 13:15:01 GMT Expires: Thu, 26 Oct 1995 00:00:00 GMT Last-Modified: Sat, 05 Sep 2009 13:15:01 GMT Pragma: no-cache Content-Length: 156 Server: Allegro-Software-RomPager/4.3"

Dan

Re: PHP Socket response

Posted: Sat Sep 05, 2009 12:44 pm
by Darhazer
Pretty interesting, the length of the headers is 258, which with 4 bytes delimiter (\r\n\r\n) and 156 bytes content makes exactly 418 bytes. I'm sure the problem is with the displaying.
Put $response in file, and let us see the result

Code: Select all

file_put_contents('response.txt', $response);

Re: PHP Socket response

Posted: Sun Sep 06, 2009 6:45 am
by danturn
rah!

its in the file... nice work!... how do i get it to put all of this (or ideally just the XML!) into a variable (i need to use regex to strip some stuff out of it)

HTTP/1.1 200 OK
Content-Type: text/xml; charset="utf-8"
Date: Sun, 06 Sep 2009 11:43:47 GMT
Expires: Thu, 26 Oct 1995 00:00:00 GMT
Last-Modified: Sun, 06 Sep 2009 11:43:47 GMT
Pragma: no-cache
Content-Length: 156
Server: Allegro-Software-RomPager/4.34

<?xml version="1.0" encoding="iso-8859-1"?>
<CiscoIPPhoneResponse>
<ResponseItem URL="Key:Speaker" Data="Success" Status="0" />
</CiscoIPPhoneResponse>

Re: PHP Socket response

Posted: Sun Sep 06, 2009 7:06 am
by danturn
hmm im wondering if i even need to display that text... if the xml is in $response variable i should be able to just use whatever the correct code of "if inst($response, "Success") {echo "Authenticated";}" im not sure how you check a string for a substring in PHP but i'm sure i can find out easily enough!

Re: PHP Socket response

Posted: Sun Sep 06, 2009 7:33 am
by jackpf
You could use strstr, stristr (case insensitive), strpos, stripos (case insensitive)...preg_match....and so on. There are loads :)

Re: PHP Socket response

Posted: Sun Sep 06, 2009 9:57 am
by danturn
Awesome ,thanks guys,

i used:

Code: Select all

if (strpos($response, 'Status="0"') !== false) {
        echo 'Authenticated';
        }
which returns positive if the response i'm looking for is provided.

One more thing...

that post command works perfectly, i can push XML to the phone now with no issues...

i also want to be able to retrieve the phones information using an HTTP GET request...

this is the request sent when i browse to the phone with IE>..

+g'wvE{L@PE@PDlGET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: en-gb
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Host: 192.168.1.5
Connection: Keep-Alive


How can i send the same request with PHP (or just get the same response)
I've tried:

Code: Select all

        
$ip = $_POST['IP'];
        
        $post ="GET/ HTTP/1.1";
        $post.="Host: $ip\r\n";
            $post.="Connection: keep-alive";
 
but no joy, i get an error saying:

HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, POST, PUT
Content-Length: 0
Server: Allegro-Software-RomPager/4.34

This is what I'm expecting in response:

Code: Select all

HTTP/1.1 200 OK
 
Content-Type: text/html
 
Date: Sun, 06 Sep 2009 14:53:30 GMT
 
Cache-Control: no-cache
 
Expires: Thu, 26 Oct 1995 00:00:00 GMT
 
Transfer-Encoding: chunked
 
Server: Allegro-Software-RomPager/4.34
 
 
 
59e
 
<HTML>
<HEAD><META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><TITLE>Cisco Systems, Inc.</TITLE>
</HEAD><BODY bgcolor="#FFFFFF" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF" text="#003031"><TABLE BORDER="1" WIDTH="100%" HEIGHT="100%" CELLSPACING="0" CELLPADDING="0" bordercolor="#003031"><TR>
<td WIDTH="200" HEIGHT="100" ALIGN=center><A HREF="http://www.cisco.com"><IMG SRC="/FS/Logo.png"></A></TD><td HEIGHT="50" bgcolor="#003031"><p ALIGN=center><B><font color="#FFFFFF" size="6">Device Information</FONT></B><p ALIGN=center><B><font color="#FFFFFF" size="4">Cisco Unified IP Phone CP-7970G ( SEP00152B036727 )</FONT></FONT></B></TD>
</TR>
<TR><td WIDTH="200" ALIGN=center VALIGN=top bgcolor="#003031"><TABLE BORDER="0" CELLSPACING="10" CELLPADDING="0"><TR>
<TD><a href="/CGI/Java/Serviceability?adapter=device.statistics.device">Device Information</A></TD></TR>
<TR>
<TD><a href="/CGI/Java/Serviceability?adapter=device.statistics.configuration">Network Configuration</A></TD></TR>
<TR><TD><B><font color='#FFFFFF'>Network Statistics</FONT></B></TD></TR><TR>
<TD>&nbsp;&nbsp;&nbsp;<a href="/CGI/Java/Serviceability?adapter=device.statistics.ethernet">Ethernet Information</A></TD>
</TR><TR><TD>&nbsp;&nbsp;&nbsp;<a href="/CGI/Java/Serviceability?adapter=device.statistics.port.access">Access</A></TD></TR><TR><TD>&nbsp;&nbsp;&nbsp;<a href="/CGI/Java/Serviceability?adapter=device.statistics.port.network">Netwo
 
59e
 
rk</A></TD></TR><TR><TD><B><font color='#FFFFFF'>Device Logs</FONT></B></TD></TR><TR>
<TD>&nbsp;&nbsp;&nbsp;<a href="/CGI/Java/Serviceability?adapter=device.statistics.consolelog">Console Logs</A></TD><TR>
<TD>&nbsp;&nbsp;&nbsp;<a href="/CGI/Java/Serviceability?adapter=device.statistics.coredumps">Core Dumps</A></TD><TR>
<TD>&nbsp;&nbsp;&nbsp;<a href="/CGI/Java/Serviceability?adapter=device.settings.status.messages">Status Messages</A></TD></TR>
<TR>
<TD>&nbsp;&nbsp;&nbsp;<a href="/CGI/Java/Serviceability?adapter=device.trace.display.alarm">Debug Display</A></TD></TR>
<TR><TD><B><font color='#FFFFFF'>Streaming Statistics</FONT></B></TD></TR><TR><TD>&nbsp;&nbsp;&nbsp;<a href="/CGI/Java/Serviceability?adapter=device.statistics.streaming.0">Stream 1 </A></TD></TR><TR><TD>&nbsp;&nbsp;&nbsp;<a href="/CGI/Java/Serviceability?adapter=device.statistics.streaming.1">Stream 2 </A></TD></TR><TR><TD>&nbsp;&nbsp;&nbsp;<a href="/CGI/Java/Serviceability?adapter=device.statistics.streaming.2">Stream 3 </A></TD></TR><TR><TD>&nbsp;&nbsp;&nbsp;<a href="/CGI/Java/Serviceability?adapter=device.statistics.streaming.3">Stream 4 </A></TD></TR><TR><TD>&nbsp;&nbsp;&nbsp;<a href="/CGI/Java/Serviceability?adapter=device.statistics.streaming.4">Stream 5 </A></TD></TR></TABLE>
</TD>
<td VALIGN=top><DIV ALIGN=center>
<TABLE BORDER="0" CELLSPACING="10" CELLPADDING="0"><TR><TD><B> MAC Address</B></TD><td width=20></TD><TD><B>00152B036727</B></TD><
 
59e
 
/TR><TR><TD><B> Host Name</B></TD><td width=20></TD><TD><B>SEP00152B036727</B></TD></TR><TR><TD><B> Phone DN</B></TD><td width=20></TD><TD><B>10014535</B></TD></TR><TR><TD><B> App Load ID</B></TD><td width=20></TD><TD><B>jar70sccp.8-4-1ES10.sbn</B></TD></TR><TR><TD><B> Boot Load ID</B></TD><td width=20></TD><TD><B>7970_64060118.bin</B></TD></TR><TR><TD><B> Version</B></TD><td width=20></TD><TD><B>SCCP70.8-4-1SR2S</B></TD></TR><TR><TD><B> Expansion Module 1</B></TD><td width=20></TD><TD><B></B></TD></TR><TR><TD><B> Expansion Module 2</B></TD><td width=20></TD><TD><B></B></TD></TR><TR><TD><B> Hardware Revision</B></TD><td width=20></TD><TD><B>1.1</B></TD></TR><TR><TD><B> Serial Number</B></TD><td width=20></TD><TD><B>INM09321ZK9</B></TD></TR><TR><TD><B> Model Number</B></TD><td width=20></TD><TD><B>CP-7970G</B></TD></TR><TR><TD><B> Message Waiting</B></TD><td width=20></TD><TD><B>Yes</B></TD></TR><TR><TD><B> UDI</B></TD><td width=20></TD><TD><B>phone</B></TD></TR><TR><TD><B> </B></TD><td width=20></TD><TD><B>Cisco Unified IP Phone 7970G, Global</B></TD></TR><TR><TD><B> </B></TD><td width=20></TD><TD><B>CP-7970G</B></TD></TR><TR><TD><B> </B></TD><td width=20></TD><TD><B></B></TD></TR><TR><TD><B> </B></TD><td width=20></TD><TD><B>INM09321ZK9</B></TD></TR><TR><TD><B> Time</B></TD><td width=20></TD><TD><B>15:53:30</B></TD></TR><TR><TD><B> Time Zone</B></TD><td width=20></TD><TD><B>GMT Standard/Daylight Time</B></TD></TR>
 
076
 
<TR><TD><B> Date</B></TD><td width=20></TD><TD><B>06/09/09</B></TD></TR></TABLE></DIV></TD></TR></TABLE></BODY></HTML>
 
0
 
 

Re: PHP Socket response

Posted: Sun Sep 06, 2009 1:43 pm
by Darhazer
You are missing a space between the method and the resource, this is why the web server accepts GET/ as a method and returns 'not allowed'

Code: Select all

$ip = $_POST['IP'];
       
        $post ="GET / HTTP/1.1\r\n";
        $post.="Host: $ip\r\n";
            $post.="Connection: keep-alive";

Re: PHP Socket response

Posted: Tue Sep 08, 2009 4:57 am
by danturn
Thanks man,

All working beautifully...

You're a life saver

Dan