Jump to content

Writing HTTP Requests


Recommended Posts

I'm writing a simple MSN client in PHP. Theres no real need to get into details about how the protocol works, its just a bunch of HTTP Soap requests. However I'm having a strange problem getting such requests to work in PHP.

 

Getting your MSN membership requires you to send this exact request to contacts.msn.com (port 80)

 

POST /abservice/SharingService.asmx HTTP/1.1
SOAPAction: http://www.msn.com/webservices/AddressBook/FindMembership
Content-Type: text/xml; charset=utf-8
Cookie: MSPAuth=<security ticket>
Host: contacts.msn.com
Content-Length: x

<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
       <ABApplicationHeader xmlns="http://www.msn.com/webservices/AddressBook">
           <ApplicationId xmlns="http://www.msn.com/webservices/AddressBook">CFE80F9D-180F-4399-82AB-413F33A1FA11</ApplicationId>
           <IsMigration xmlns="http://www.msn.com/webservices/AddressBook">false</IsMigration>
           <PartnerScenario xmlns="http://www.msn.com/webservices/AddressBook">Initial</PartnerScenario>
       </ABApplicationHeader>
       <ABAuthHeader xmlns="http://www.msn.com/webservices/AddressBook">
           <ManagedGroupRequest xmlns="http://www.msn.com/webservices/AddressBook">false</ManagedGroupRequest>
       </ABAuthHeader>
   </soap:Header>
   <soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
       <FindMembership xmlns="http://www.msn.com/webservices/AddressBook">
           <serviceFilter xmlns="http://www.msn.com/webservices/AddressBook">
               <Types xmlns="http://www.msn.com/webservices/AddressBook">
                   <ServiceType xmlns="http://www.msn.com/webservices/AddressBook">Messenger</ServiceType>
                   <ServiceType xmlns="http://www.msn.com/webservices/AddressBook">Invitation</ServiceType>
                   <ServiceType xmlns="http://www.msn.com/webservices/AddressBook">SocialNetwork</ServiceType>
                   <ServiceType xmlns="http://www.msn.com/webservices/AddressBook">Space</ServiceType>
                   <ServiceType xmlns="http://www.msn.com/webservices/AddressBook">Profile</ServiceType>
               </Types>
           </serviceFilter>
       </FindMembership>
   </soap:Body>
</soap:Envelope>

 

Easy enough right?

 

Well not so easy in php. Here is what I have.

 


	$this->membershipBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	$this->membershipBody .= "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
	$this->membershipBody .= "<soap:Header xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
	$this->membershipBody .= "<ABApplicationHeader xmlns=\"http://www.msn.com/webservices/AddressBook\">";
	$this->membershipBody .= "<ApplicationId xmlns=\"http://www.msn.com/webservices/AddressBook\">CFE80F9D-180F-4399-82AB-413F33A1FA11</ApplicationId>";
	$this->membershipBody .= "<IsMigration xmlns=\"http://www.msn.com/webservices/AddressBook\">false</IsMigration>";
	$this->membershipBody .= "<PartnerScenario xmlns=\"http://www.msn.com/webservices/AddressBook\">Initial</PartnerScenario>";
	$this->membershipBody .= "</ABApplicationHeader>";
	$this->membershipBody .= "<ABAuthHeader xmlns=\"http://www.msn.com/webservices/AddressBook\">";
	$this->membershipBody .= "<ManagedGroupRequest xmlns=\"http://www.msn.com/webservices/AddressBook\">false</ManagedGroupRequest>";
	$this->membershipBody .= "</ABAuthHeader>";
	$this->membershipBody .= "</soap:Header>";
	$this->membershipBody .= "<soap:Body xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
	$this->membershipBody .= "<FindMembership xmlns=\"http://www.msn.com/webservices/AddressBook\">";
	$this->membershipBody .= "<serviceFilter xmlns=\"http://www.msn.com/webservices/AddressBook\">";
	$this->membershipBody .= "<Types xmlns=\"http://www.msn.com/webservices/AddressBook\">";
	$this->membershipBody .= "<ServiceType xmlns=\"http://www.msn.com/webservices/AddressBook\">Messenger</ServiceType>";
	$this->membershipBody .= "<ServiceType xmlns=\"http://www.msn.com/webservices/AddressBook\">Invitation</ServiceType>";
	$this->membershipBody .= "<ServiceType xmlns=\"http://www.msn.com/webservices/AddressBook\">SocialNetwork</ServiceType>";
	$this->membershipBody .= "<ServiceType xmlns=\"http://www.msn.com/webservices/AddressBook\">Space</ServiceType>";
	$this->membershipBody .= "<ServiceType xmlns=\"http://www.msn.com/webservices/AddressBook\">Profile</ServiceType>";
	$this->membershipBody .= "</Types>";
	$this->membershipBody .= "</serviceFilter>";
	$this->membershipBody .= "</FindMembership>";
	$this->membershipBody .= "</soap:Body>";
	$this->membershipBody .= "</soap:Envelope>";

function getMembership () 
{
	$head = "POST /abservice/SharingService.asmx HTTP/1.1\r\n";
	$head .= "SOAPAction: http://www.msn.com/webservices/AddressBook/FindMembership\r\n";
	$head .= "Content-Type: text/xml; charset=utf-8\r\n";
	$head .= "Cookie: MSPAuth=".$this->loginpt->getAuth()."\r\n";
	$head .= "Host: contacts.msn.com\r\n";
	$head .= "Content-Length: ".strlen($this->membershipBody)."\r\n";
	$head .= "\r\n";

	$sock = fsockopen ($this->membershipServer, 80);
	$content = $head.$this->membershipBody;
	fwrite ($sock, $content);

               echo (fgets());
}

 

Sending this off gets me a 400 HTTP Error (bad request). I've checked what $this->loginpt->getAuth() returns and it returns the appropriate authentication code. This is really weird, because the Java implementation I tried does worked. There is no problem with the protocol data, it is all directly copy paste from Java program that works. I used the exact same strings and used the exact same method (open socket, print data to socket, read results). Does PHP handle socket connections in a different way? Is there a more appropriate way of to sending HTTP Requests?

 

Link to comment
https://forums.phpfreaks.com/topic/164582-writing-http-requests/
Share on other sites

Archived

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

×
×
  • Create New...

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.