Friday, February 27, 2015

Easy way to send notification on iPhone in php

Notification on iPhone:

This is very easy & effective way in php to send a notification on your iPhone.

For this you will be in need of three things.

1. Your iPhone's device token
2. .pem file for iPhone
3. Its passphrase.

For tihs use the following code.

        $deviceToken = 'enter_your_device_token';
        $passphrase = 'enter_your_passphrase';
        $message = 'enter_your_notification_message';

        $badge = 1;
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'path_to_pem_file.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

        // Open a connection to the APNS server
        $fp = stream_socket_client(
            'ssl://gateway.sandbox.push.apple.com:2195', $err,
            $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
        if (!$fp)
            exit("Failed to connect: $err $errstr" . PHP_EOL);
        echo 'Connected to APNS' . PHP_EOL;

        // Create the payload body
        $body['aps'] = array(
            'alert' => $message,
            'badge' => $badge,
            'sound' => 'newMessage.wav'
        );

        // Encode the payload as JSON
        $payload = json_encode($body);

        // Build the binary notification
        $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

        // Send it to the server
        $result = fwrite($fp, $msg, strlen($msg));

        if (!$result)
            echo 'Error, notification not sent' . PHP_EOL;
        else
            echo 'notification sent!' . PHP_EOL;

        // Close the connection to the server
        fclose($fp);


Thanks.

Friday, February 20, 2015

How to get Latitude & Longitude using zipcode in php?

Get Latitude & Longitude:

It is a very simple way to get latitude & longitude using zipcode in php.

For this you have to use google api's.

Here is a function for doing this:

public function getLatLng($zip)
    {
        $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=$zip&sensor=true";
        $xml = simplexml_load_file($request_url) or die("url not loading");
        $status = $xml->status;
        $lat = $lng = 0;
        if ($status == "OK") {
            $lat = $xml->result->geometry->location->lat;
            $lng = $xml->result->geometry->location->lng;
        }
        $loc = array('lat' => $lat, 'lng' => $lng);
        return $loc;
    }


Here $zip is a zipcode variable. It will return a array consisting of latitude & longitude.

Thanks.

Friday, February 6, 2015

How to get yelp rating through business id in php

Get Yelp Rating :

For getting yelp rating firstly you will be in need to register your developer account in yelp. Some times you can't be register, Because yelp provide its registration for some selected countries. For this you have to use proxy ip of those countries.

Due to registration you will get

$CONSUMER_KEY = '';
$CONSUMER_SECRET = '';
$TOKEN = '';
$TOKEN_SECRET = '';   

$API_HOST = 'api.yelp.com';
$BUSINESS_PATH = '/v2/business/';
 

 
 You have to include this file at the top of your coding


Add these functions

 function request($host, $path) {
$unsigned_url = "http://" . $host . $path;
// Token object built using the OAuth library
$token = new OAuthToken($GLOBALS['TOKEN'], $GLOBALS['TOKEN_SECRET']);
// Consumer object built using the OAuth library
$consumer = new OAuthConsumer($GLOBALS['CONSUMER_KEY'], $GLOBALS['CONSUMER_SECRET']);
// Yelp uses HMAC SHA1 encoding
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$oauthrequest = OAuthRequest::from_consumer_and_token(
$consumer,
$token,
'GET',
$unsigned_url
);
// Sign the request
$oauthrequest->sign_request($signature_method, $consumer, $token);
// Get the signed URL
$signed_url = $oauthrequest->to_url();
// Send Yelp API Call
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}


Now Get Business id and call this function

function get_business($business_id) {
$business_path = $GLOBALS['BUSINESS_PATH'] . $business_id;
return request($GLOBALS['API_HOST'], $business_path);
}



 Thats It.

Now If your business id is z-and-y-restaurant-san-francisco-3





You will easily get its rating.