Ask a questionAsk a question
 

AnswerWindows Live Admin Centre SDK: Help with PHP please?

  • Saturday, June 28, 2008 7:15 AMa JD  Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

    After looking around on the web, I was having major difficulty trying to find anything of use, So ill post this here in the hope that Search engines index this for others (A good PHP starting point?)

    However, I am having difficulty doing anything after the login is done, I can verify it, But something like TestConnection i'm unable to do... I've tried adding the headers, with no luck.

    My other thought was to add the cookie (which PHP also sets for every request after that... but that didnt work...


    Could someone give me a pointer or perhaps even a sample function of "TestConnection" with the headers set?

    I appreciate your time, Thanks

    Joshua "JD" Davison.

    Requires: PHP5 w/SOAP.

    Code Snippet

    <?php
    $client = new SoapClient("https://domains.live.com/service/managedomain2.asmx?WSDL");

    $WL->User = "example@domain.com";
    $WL->Pass = "mYpAsSwOrD";

    $a1 = GetLoginUrl($WL->User);
    $a2 = str_replace(array("%NAME%","%PASSWORD%"),array($WL->User,$WL->Pass),GetLoginDataTemplate());
    $a3 = GetTicket($a1,$a2);
    $client->__setCookie("RPSTAuth",$WL->ticket);
    $a4 = VerifyAuthData($WL->authData);
    $a5 = TestConnection(array("testss"));
    echo $client->__getLastResponseHeaders()."\r\n".$client->__getLastResponse();
    //$a6 = EnumDomains();

     

    function GetLoginUrl($memberName) {
     global $client;
     return $client->GetLoginUrl(array("memberNameIn"=>$memberName))->GetLoginUrlResult;
    }

     

    function GetLoginDataTemplate() {
     global $client;
     return $client->GetLoginDataTemplate()->GetLoginDataTemplateResult;
    }

     

    function VerifyAuthData($authData) {
     global $client;
     return ($client->VerifyAuthData(array("authData"=>$authData))->VerifyAuthDataResult==1?"true":"false");
    }

     

    function TestConnection($testString) {
     global $client;
     echo $client->TestConnection(array("name"=>$testString))->TestConnectionResult;
    }

     

    //Custom Functions Below.

    function doPost($url, $data, $optional_headers = null) {
     $params = array('http' => array('method' => 'POST','content' => $data));
     if ($optional_headers !== null) {
      $params['http']['header'] = $optional_headers;
     }
     $ctx = stream_context_create($params);
     $fp = @fopen($url, 'rb', false, $ctx);
     if (!$fp) { return; }
     $response = @stream_get_contents($fp);
     return $response;
    }

     

    function GetTicket($loginUrl,$loginData) {
     //Returns true for successful login.
     global $WL;
     $postReply = doPost($loginUrl,$loginData);
     if (!$postReply) { return "false"; }
     $p = xml_parser_create();
     $result = xml_parse_into_struct($p,$postReply,$vals,$index);
     xml_parser_free($p);
     $WL->authData = $postReply;
     $WL->ticket = ereg_replace("^.*&t=(.*)&lc=.*$","\\1",$vals[$index[REDIRECT][0]][value]);
     return $vals[$index[LOGINRESPONSE][0]][attributes][SUCCESS];
    }
    ?>

     

     

Answers

  • Monday, July 07, 2008 7:33 PMBen Williams [MSFT] Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I'm not a PHP guru but just doing I just did some quick searching and found a number of samples that look like they should work for you. The key is the __SetSoapHeaders method.

     

    Any example you find should do the trick for you except instead of Username and Passwords, the soapheader members need to be authorizationType and authorizationData.

     

    -Ben

     

    btw - you should also pull down a local copy of that WSDL file to reference instead of always hitting it when you build the client proxy. That way you dont make an extra network call each time you run the app.

     

  • Friday, July 18, 2008 8:11 AMMauricioC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Here you have a working example on PHP 5 with SOAP. You have to use cURL to do the initial sing-in:

    http://www.mauriciocuenca.com/blog/2008/07/live-admin-center-php-client.html

All Replies

  • Monday, July 07, 2008 6:37 PMBen Williams [MSFT] Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The most common issue with this sort of question is getting the SOAP authorization headers incorrect. Do an http trace of your request and compare it to the one in this thread http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=3306487&SiteID=1

     

    That thread has a complete XML dump of a functioning TestConnection call (minus the actual token of course ) You should be able to compare the two. If you dont see it then post your XML back to this thread and I'll have a look at it.

  • Monday, July 07, 2008 6:40 PMa JD  Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Ben, Thought I'd never get a reply!

    I know thats the problem (you're right), But I have no idea how to add header to PHP5's SOAP 
  • Monday, July 07, 2008 7:33 PMBen Williams [MSFT] Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I'm not a PHP guru but just doing I just did some quick searching and found a number of samples that look like they should work for you. The key is the __SetSoapHeaders method.

     

    Any example you find should do the trick for you except instead of Username and Passwords, the soapheader members need to be authorizationType and authorizationData.

     

    -Ben

     

    btw - you should also pull down a local copy of that WSDL file to reference instead of always hitting it when you build the client proxy. That way you dont make an extra network call each time you run the app.

     

  • Friday, July 18, 2008 8:11 AMMauricioC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Here you have a working example on PHP 5 with SOAP. You have to use cURL to do the initial sing-in:

    http://www.mauriciocuenca.com/blog/2008/07/live-admin-center-php-client.html
  • Friday, July 18, 2008 1:58 PMa JD  Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Very nice. will toy with it soon Smile thanks.
  • Wednesday, October 28, 2009 1:12 AMfedericar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Working PHP class for Windows Live Admin Center 4.1

    modified from original version from Mauricio Cuenca 

    <?php

    class WinAdminCenter {
        private $username;
        private $password;
        private $wsdlfile = 'https://domains.live.com/service/managedomain2.asmx?WSDL';
    private $namespace = "http://domains.live.com/Service/ManageDomain/V1.0";
        private $soapver = SOAP_1_2;
    private $header;
    private $headerbody;

        /**
         * Constructor
         * @param string $username
         * @param string $password
         * @return resource
         */
        public function __construct($username, $password)
        {
            $this->username = $username;
            $this->password = $password;
            $this->client = new SoapClient($this->wsdlfile, array('soap_version' => $this->soapver));
        }

        /**
         * Obtiene el URL de autenticación
         * @param string $membername
         * @return string
        */
        private function getLoginURL($membername)
        {
            $obj = $this->client->GetLoginURL(array('memberNameIn' => $membername));
            return $obj->GetLoginUrlResult;
        }

        /**
         * Obtiene el template que se envía al Webservice
         * @param string $username
         * @param string $password
         * @return string
        */
        private function getLoginDataTemplate($username, $password)
        {
            $template = $this->client->GetLoginDataTemplate();
            $template = str_replace(array('%NAME%', '%PASSWORD%'),
           array($this->username, $this->password),
           $template->GetLoginDataTemplateResult);
            return $template;
        }

        /**
         * Obtiene los datos de autenticación usados en todas las peticiones
         * @return string
         */
        private function getAuthData()
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $this->getLoginURL($this->username));
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $this->getLoginDataTemplate($this->username, $this->password));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_AUTOREFERER, true);
            //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

            return trim(curl_exec($ch));
        }

        private function setHeader()
    {
    //Body of the Soap Header. 
    $this->headerbody = array('authorizationType' => 'PassportTicket', 
                         'authorizationData' => $this->getAuthData() ); 
    //Create Soap Header.
    $this->header = new SOAPHeader($this->namespace, 'ManageDomain2Authorization', $this->headerbody);        
    //set the Headers of Soap Client. 
    $this->client->__setSoapHeaders($this->header);
    }

        /**
         * Revisa si el usuario existe o no
         * @param string $username
         * @return string
         */
        public function getMemberInfo($username)
        {
            try {
    $this->setHeader();
                $result = $this->client->GetMemberInfo(array('memberNameIn' => $username));
                return $result->GetMemberInfoResult;
            } catch (SoapFault $sf) {
                trigger_error("SOAP Fault: (code: {$sf->faultcode},
                message: {$sf->faultstring})", E_USER_ERROR);
            }
        }


        /*************************/
    function TestConnection($testString) {
    try {
    $this->setHeader();
    return $this->client->TestConnection(array("name"=>$testString))->TestConnectionResult;
    } catch (SoapFault $sf) {
                trigger_error("SOAP Fault: (code: {$sf->faultcode},
                message: {$sf->faultstring})", E_USER_ERROR);
    }
    }
        /*************************/
        public function enumMembers($domainName, $start="", $num="")
        {
            $userdata = array('domainName' => $domainName,
      'start' => $start,
      'num' => $num);

            try {
    $this->setHeader();
                return $this->client->EnumMembers($userdata);
            } catch (SoapFault $sf) {
                trigger_error("SOAP Fault: (code: {$sf->faultcode},
                message: {$sf->faultstring})", E_USER_ERROR);
            }
        }

        /*************************/
        public function getMemberCount($domainName)
        {
            $userdata = array('domainName' => $domainName,
     'authData' => $this->getAuthData(),
     );

            try {
    $this->setHeader();
                return $this->client->GetMemberCount($userdata);
            } catch (SoapFault $sf) {
                trigger_error("SOAP Fault: (code: {$sf->faultcode},
                message: {$sf->faultstring})", E_USER_ERROR);
            }
        }


        /*************************/
        public function getMemberNameState($memberNameIn)
        {
            $userdata = array('memberNameIn' => $memberNameIn
     );

            try {
    $this->setHeader();
                return $this->client->GetMemberNameState($userdata);
            } catch (SoapFault $sf) {
                trigger_error("SOAP Fault: (code: {$sf->faultcode},
                message: {$sf->faultstring})", E_USER_ERROR);
            }
        }


        /*************************/ 
        public function createMember($memberNameIn, $password, $resetPassword, $firstName, $lastName, $lcid)
        {
            $userdata = array('memberNameIn' => $memberNameIn,
     'password' => $password,
     'resetPassword' => $resetPassword,
     'firstName' => $firstName,
     'lastName' => $lastName,
     'lcid' => $lcid
     );

            try {
    $this->setHeader();
                return $this->client->CreateMember($userdata);
            } catch (SoapFault $sf) {
                //trigger_error("SOAP Fault: (code: {$sf->faultcode}, message: {$sf->faultstring})", E_USER_NOTICE);
    return $sf->faultstring;
            }
        }


        /*************************/
    /*
        public function CreateMemberWithPropertiesEx($memberNameIn, $password, $resetPassword, $sq, $sa, $resetSQ, $alternateEmail)
        {
            $userdata = array('memberNameIn' => $memberNameIn,
     'password' => $password,
     'resetPassword' => $resetPassword,
     'sq' => $sq,
     'sa' => $sa,
     'resetSQ' => $resetSQ,
     'alternateEmail' => $alternateEmail
     );

            try {
    $this->setHeader();
                return $this->client->CreateMemberWithPropertiesEx($userdata);
            } catch (SoapFault $sf) {
                trigger_error("SOAP Fault: (code: {$sf->faultcode},
                message: {$sf->faultstring})", E_USER_ERROR);
            }
        }
    */
        /*************************/
        public function MemberNameToNetId($memberNameIn)
        {
            $userdata = array('memberNameIn' => $memberNameIn
     );

            try {
    $this->setHeader();
                return $this->client->MemberNameToNetId($userdata);
            } catch (SoapFault $sf) {
                //trigger_error("SOAP Fault: (code: {$sf->faultcode}, message: {$sf->faultstring})", E_USER_ERROR);
    return $sf->faultstring;
            }
        }
        /*************************/
        public function NetIdToMemberName($netIdIn)
        {
            $userdata = array('netIdIn' => $netIdIn
     );

            try {
    $this->setHeader();
                return $this->client->NetIdToMemberName($userdata);
            } catch (SoapFault $sf) {
                //trigger_error("SOAP Fault: (code: {$sf->faultcode}, message: {$sf->faultstring})", E_USER_ERROR);
    return $sf->faultstring;
            }
        }

    }

    ?>