Showing posts with label server. Show all posts
Showing posts with label server. Show all posts

Wednesday, July 23, 2014

Php & MySql oAuth Server & Client Example

Server side common.php (Need to include in all server side scripts)


<?php
require_once 'oauthLibrary/OAuthServer.php';
session_start();
 
// Add a header indicating this is an OAuth server
header('X-XRDS-Location: http://' . $_SERVER['SERVER_NAME'] . '/services.xrds.php');
 
// Connect to database
$db = new PDO('mysql:host=localhost;dbname=oauthdb', 'root', '');
 
// Create a new instance of OAuthStore and OAuthServer
$store = OAuthStore::instance('PDO', array('conn' => $db));
$server = new OAuthServer();
OAuthRequestLogger::enableLogging($store);
?>

Client side common.php (Need to include in all client side scripts)


<?php
session_start();

define("PATH_URL_CLIENT", "/oauth/client");
define("PATH_URL_SERVER", "/oauth/server");

define("REQUEST_TOKEN", "REQUEST_TOKENsdsdfw324ft3f3f34r34");
define("ACCESS_TOKEN", "ACCESS_TOKENkdfj33jdl23");
define("OAUTH_TOKEN", "OAUTH_TOKENdfs34fre45u67jyu");
define("OAUTH_VERIFIER", "OAUTH_VERIFIERslsjlf32j3jlfj");


require './lib/OAuthClient.php';
$client = new OAuthClient('8179e89ff6558cce5628e60643a7124c053cfc204', 'f46dfa522890df25e71ccd9db463a708');

function getServerUrl() {
    return "http://".$_SERVER["HTTP_HOST"].PATH_URL_SERVER;
}

function getClientUrl() {
    return "http://".$_SERVER["HTTP_HOST"].PATH_URL_CLIENT;
}

function toSession($key, $value) {
    $_SESSION[$key] = is_null($value) ? $value : serialize($value);
}

function fromSession($key) {
    if(isset($_SESSION[$key]) && !is_null($_SESSION[$key])) {
        return unserialize($_SESSION[$key]);
    }
    return null;
}
?>

1. Browse http://localhost/oauth/server/registration.html and fill up all fields.


2. You will be view the following information which will need to communication to server.


3. Browse the following url to get the token url, copy the url and browse

4. If you logged in success in server url, you will redirect to your callback url you specified when registration with token & verifier.


5. You are now ready to request data from server.


Download full code example.

Saturday, April 27, 2013

Php making a soap based wsdl server

First create a wsdl file under www/soap/ directory named suppose soap1.wsdl:

<?xml version="1.0"?>
<definitions name="WsdlSoap" targetNamespace="urn:WsdlSoap"
             xmlns:tns="urn:WsdlSoap" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
             xmlns="http://schemas.xmlsoap.org/wsdl/">

    <message name="getMyName">
        <part name="name" type="string"/>
    </message>
    <message name="getMyNameResponse">
        <part name="return" type="string"/>
    </message>
    <portType name="WsdlSoapPort">
        <operation name="getMyName">
            <input message="tns:getMyName"/>
            <output message="tns:getMyNameResponse"/>
        </operation>
    </portType>
    <binding name="WsdlSoapBinding" type="tns:WsdlSoapPort">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="getMyName">
            <soap:operation soapAction="urn:WsdlSoapAction"/>
            <input>
                <soap:body use="encoded" namespace="urn:WsdlSoap" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
            </input>
            <output>
                <soap:body use="encoded" namespace="urn:WsdlSoap" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
            </output>
        </operation>
    </binding>
    <service name="WsdlSoapService">
        <port name="WsdlSoapPort" binding="tns:WsdlSoapBinding">
            <soap:address location="http://localhost/soap/server1.php"/>
        </port>
    </service>
</definitions>

/soap/server1.php

<?php
function getMyName($name)
{

    return "Your name: ".$name;

}

$server = new SoapServer("soap1.wsdl");

$server->addFunction("getMyName");

$server->handle();
?>

Call soap:
$soap = new SoapClient("http://localhost/soap/soap1.wsdl");
echo $soap-> getMyName ("Pritom Kumar");
And it prints: "Your name: Pritom Kumar".