Sunday, June 30, 2013

Yahoo Oauth Login Connect Using PHP

Little Description
1. If user want to login with his yahoo account he is redirected to yahoo login page from our website for authentication.
2. After successful login yahoo issues user data along with his GUID (Globally Unique Identifier) to our website.
3. We are identifying a user using his GUID in our database. In third step we need to check our database for existence of GUID if it is present the user is old user and display his account. If GUID is not present insert a new record and allot new privileges for him by creating a new account.
4. Display user account with his data.

In order to start working with Yahoo SDK you need to register a web application and get the Application ID, Consumer keys.

1. Creating a new Application
a) Register a new application
b) Get the Application ID, Consumer Key and Consumer Secret. Setup permission level to your application as much as you need.































<?php
require 'lib/Yahoo.inc';
session_start();
/* Place Yoru Consumer Key here */ 
define('OAUTH_CONSUMER_KEY', 'your_consumer_key'); 
 
/* Place your Consumer Secret */ 
define('OAUTH_CONSUMER_SECRET', 'your_consumer_secret'); 
 
/* Place your Application Id here */ 
define('OAUTH_APP_ID', 'your_app_id');
 
/* Your return url which you provide when create application */ 
define("YAHOO_APPLICATION_URL", "http://allinone.com/yahoo.com");
 
$session = YahooSession::requireSession(
    OAUTH_CONSUMER_KEY, 
    OAUTH_CONSUMER_SECRET, 
    OAUTH_APP_ID,
    YAHOO_APPLICATION_URL
);
 
if (is_object($session))  {
    $user = $session->getSessionedUser();
    $profile = $user->getProfile();
    $status = $user->getPresence(); // Fetch the status for the current user.  
    $name = $profile->nickname; // Getting user name
    $guid = $profile->guid; // Getting Yahoo ID 
     
    $start = 0; $count = 100; $total = 0
    $connections = $user->getConnections($start, $count, $total);
 
    $updates = $user->getUpdates();  //Fetch the updates for the current user.     
    //Fetch the updates for the connections of the current user.  
    $connectionUpdates = $user->getConnectionUpdates();
}
 
/* POSTING USER ACTIVITIES */

  1.    $user = $session->getSessionedUser();  
  2.   
  3.    // set the status for the user  
  4.    $status = "is hacking";  
  5.    $user->setStatus($status);  
  6.   
  7.    // store the update data  
  8.    $title = "wrote a new blog post.";  
  9.    $description = "Lorem ipsum dolor sit amet...";  
  10.    $link = "http://pritomkumarb.blogspot.com/blog/my-awesome-blog-post";  
  11.   
  12.    // create an unique hash of the update data using md5  
  13.    $suid = md5($title.$description.$link.time());  
  14.   
  15.    // insert the update...  
  16.    $user->insertUpdate($suid, $title, $link, $description);
   
 

USING YQL

Using Yahoo! Query Language (YQL) in your applications lets you query for data across many other Yahoo! APIs and other data sources across the internet. Learn more about Yahoo! Query Language (YQL) here.
The sample query below gets the user's location and queries the GeoPlanet API in order to return structured place data.

  1.    $query = sprintf("select * from geo.places where text='ballard';");  
  2.    $response = $session->query($query);  
  3.   
  4.    print_r($response);

 
?>
Download Library File

Cloudant create view and used indexes to search values

Create a document using : "_design/views103" (After selecting a database).
Enter "indexes" as key.
Enter following content after converting as JSON to value field:

{
    "search_name": {
        "index": 
          "function(doc) {
            index("default", doc._id);
            if (doc['customerID']) {
                index("customerID", doc['customerID'], {"store": "yes"});
            }
        }"
    }
}

_id automatically used as index.

https://{name}.cloudant.com/animaldb/_design/views103/_search/animals?q={id}
Wild card searches are supported, for both single (?) and multiple (*)
character searches. dat? would match date and data, dat* would match date,
data, database, dates etc. Wildcards must come after a search term, you cannot
do a query like *base.



customerID is a key field in data object.
https://{name}.cloudant.com/transactions/_design/views103/_search/animals?q=customerID:%228547963512%22

https://cloudant.com/for-developers/views/
https://cloudant.com/for-developers/search/

With this index you can run any of these queries.
Desired resultQuery
Birds class:bird
Animals that begin with the letter "l" l*
Carnivorous birds class:bird AND diet:carnivore
Herbivores that start with letter "l" l* AND diet:herbivore
Medium-sized herbivores min_length:[1 TO 3] AND diet:herbivore
Herbivores that are 2m long or less diet:herbivore AND min_length:[-Infinity TO 2]
Mammals that are at least 1.5m long class:mammal AND min_length:[1.5 TO Infinity]
Find "Meles meles" latin_name:"Meles meles"
Mammals who are herbivore or carnivore diet:(herbivore OR omnivore) AND class:mammal

Saturday, June 29, 2013

eWay transaction using php api direct payment

/* Pay using eWay */
function pay_using_eway()
{
    $testUrl = "https://www.eway.com.au/gateway_cvn/xmltest/testpage.asp";
    $liveUrl = "https://www.eway.com.au/gateway_cvn/xmlpayment.asp";
    $eWaySOAPActionURL = "https://www.eway.com.au/gateway/managedpayment";
    $eWayCustomerId = "87654321"; /* test account */
    $eWayTotalAmount = 100; /* 1$ = 100 cent */
    $directXML = "<ewaygateway>".
        "<ewayCustomerID>".$eWayCustomerId."</ewayCustomerID>".
        "<ewayTotalAmount>".$eWayTotalAmount."</ewayTotalAmount>".
        "<ewayCustomerFirstName></ewayCustomerFirstName>".
        "<ewayCustomerLastName></ewayCustomerLastName>".
        "<ewayCustomerEmail></ewayCustomerEmail>".
        "<ewayCustomerAddress></ewayCustomerAddress>".
        "<ewayCustomerPostcode></ewayCustomerPostcode>".
        "<ewayCustomerInvoiceDescription></ewayCustomerInvoiceDescription>".
        "<ewayCustomerInvoiceRef> Invoice Reference </ewayCustomerInvoiceRef>".
        "<ewayCardHoldersName>Card Holder Name</ewayCardHoldersName>".
        "<ewayCardNumber>4444333322221111</ewayCardNumber>".
        "<ewayCardExpiryMonth>01</ewayCardExpiryMonth>".
        "<ewayCardExpiryYear>2015</ewayCardExpiryYear>".
        "<ewayCVN>123</ewayCVN>".
        "<ewayTrxnNumber></ewayTrxnNumber>".
        "<ewayOption1></ewayOption1>".
        "<ewayOption2></ewayOption2>".
        "<ewayOption3></ewayOption3>".
    "</ewaygateway>";

    $result = makeCurlCall(
        $testUrl, /* CURL URL */
        "POST", /* CURL CALL METHOD */
        array( /* CURL HEADERS */
            "Content-Type: text/xml; charset=utf-8",
            "Accept: text/xml",
            "Pragma: no-cache",
            "SOAPAction: ".$eWaySOAPActionURL,
            "Content_length: ".strlen(trim($directXML))
        ),
        null, /* CURL GET PARAMETERS */
        $directXML /* CURL POST PARAMETERS AS XML */
    );

    if($result != null && isset($result["response"])) {
        $response = new SimpleXMLElement($result["response"]);
        $response = simpleXMLToArray($response);
        print_r2($response);
    }
    die("");
}

/* makeCurlCall */
function makeCurlCall($url, $method = "GET", $headers = null, $gets = null, $posts = null) {
    $ch = curl_init();
    if($gets != null)
    {
        $url.="?".(http_build_query($gets));
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    if($posts != null)
    {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $posts);
    }
    if($method == "POST") {
        curl_setopt($ch, CURLOPT_POST, true);
    } else if($method == "PUT") {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    } else if($method == "HEAD") {
        curl_setopt($ch, CURLOPT_NOBODY, true);
    }
    if($headers != null && is_array($headers))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    $response = curl_exec($ch);
    $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

    curl_close($ch);
    return array(
        "code" => $code,
        "response" => $response
    );
}

/* Response */ 
Array
(
    [ewayTrxnStatus] => Array
        (
            [__cdata] => True
        )

    [ewayTrxnNumber] => Array
        (
            [__cdata] => 20466
        )

    [ewayTrxnReference] => Array
        (
            [__cdata] => 
        )

    [ewayTrxnOption1] => Array
        (
            [__cdata] => 
        )

    [ewayTrxnOption2] => Array
        (
            [__cdata] => 
        )

    [ewayTrxnOption3] => Array
        (
            [__cdata] => 
        )

    [ewayAuthCode] => Array
        (
            [__cdata] => 123456
        )

    [ewayReturnAmount] => Array
        (
            [__cdata] => 100
        )

    [ewayTrxnError] => Array
        (
            [__cdata] => 00,Transaction Approved(Test CVN Gateway)
        )

)
 
http://www.eway.com.au/developers/api/direct-payments 

Friday, June 28, 2013

Window open() popup Method


Parent window

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<span id="popup"> Click to Open Popup </span>

<script type="text/javascript">
var ar=new Array("Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6",
                 "Item 7", "Item 8", "Item 9", "Item 10");
function getArray(){
    return ar;
}
function changeHere(value) {
    console.log(value);
    $("span#popup").append("<div>"+value+"</div>");
}
$(document).ready(function(){
    $("span#popup").click(function(){
        var p=window.open("wind2.php", 'popUpWindow','height=400, width=650, left=300, top=100, resizable=yes, scrollbars=yes, toolbar=yes, menubar=no, location=no, directories=no, status=yes');
    });
});

</script>

Child window

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<ul id="list"></ul>
<script type="text/javascript">
    jQuery(document).ready(function() {
        if(window.opener && !window.opener.closed) {
            alert("LOADED");
        }
        if(window.opener && !window.opener.closed){
            var ar= window.opener.getArray();
            var items="";
            for(var i=0;i<ar.length;i++){
                    items +="<li>" + ar[i] + "</li>";
            }
            $("ul#list").html(items);
            window.opener.changeHere("From child window.");
        } else {
            alert("No window opener");
            window.location.href = 'wind.php';
        }
        //window.close();
    });
</script>
<?phpecho "<pre>";print_r($_REQUEST);
echo 
"</pre>";?>

Parameter Description
URL Optional. Specifies the URL of the page to open. If no URL is specified, a new window with about:blank is opened
name Optional. Specifies the target attribute or the name of the window. The following values are supported:
  • _blank - URL is loaded into a new window. This is default
  • _parent - URL is loaded into the parent frame
  • _self - URL replaces the current page
  • _top - URL replaces any framesets that may be loaded
  • name - The name of the window
specs Optional. A comma-separated list of items. The following values are supported:

channelmode=yes|no|1|0 Whether or not to display the window in theater mode. Default is no. IE only
directories=yes|no|1|0 Whether or not to add directory buttons. Default is yes. IE only
fullscreen=yes|no|1|0 Whether or not to display the browser in full-screen mode. Default is no. A window in full-screen mode must also be in theater mode. IE only
height=pixels The height of the window. Min. value is 100
left=pixels The left position of the window
location=yes|no|1|0 Whether or not to display the address field. Default is yes
menubar=yes|no|1|0 Whether or not to display the menu bar. Default is yes
resizable=yes|no|1|0 Whether or not the window is resizable. Default is yes
scrollbars=yes|no|1|0 Whether or not to display scroll bars. Default is yes
status=yes|no|1|0 Whether or not to add a status bar. Default is yes
titlebar=yes|no|1|0 Whether or not to display the title bar. Ignored unless the calling application is an HTML Application or a trusted dialog box. Default is yes
toolbar=yes|no|1|0 Whether or not to display the browser toolbar. Default is yes
top=pixels The top position of the window. IE only
width=pixels The width of the window. Min. value is 100

replace Optional.Specifies whether the URL creates a new entry or replaces the current entry in the history list. The following values are supported:
  • true - URL replaces the current document in the history list
  • false - URL creates a new entry in the history list

Thursday, June 27, 2013

jQuery/Javascript to replace broken images

Handle the onError event for the image to reassign its source using JavaScript:
function(image) {
    image.onerror = "";
    image.src = "/images/noimage.gif";
    return true;
}
<img src="image.png" onerror="imgError(this);"/>
Or without a JavaScript function:
<img src="image.png" onError="this.onerror=null;this.src='/images/noimage.gif';" />

jQuery event for images loaded

http://api.jquery.com/load-event/
This method is a shortcut for .on('load', handler).
The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.
For example, consider a page with a simple image:

<img src="image1.png" alt="Image Alt Tag" id="image1" />

$('#image1')
 .load(function(){
  $('#result1').text('Image is loaded!'); 
 })
 .error(function(){
  $('#result1').text('Image is not loaded!');
 });