Tuesday, 13 December 2016

REST service in regional language using utf8

When we want to display regional language from the database , we have to use utf8 formate

so 

mysqli_set_charset($conn,"utf8");

this line of the code must be added after the connection before the SQL


<?php

  ini_set('display_errors', 1);  
 ini_set('display_startup_errors', 1);
 error_reporting(E_ALL);
 //header("Content-Type: text/html;charset=UTF-8");
 $mysql_hostname = '*************';
 $mysql_user = '******';
 $mysql_password = '*********';
 $mysql_database = '**********';
 //echo "*************************************";
 //var_dump(function_exists('mysqli_connect'));
 $conn = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
 if (!$conn){    
 die("Connection error: " . mysqli_connect_error());  
 }
 // Change character set to utf8
mysqli_set_charset($conn,"utf8");
 $sql = "SELECT * FROM  wp_bestcinemaposts ";
 $rows = array();
 $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
 while ($row = mysqli_fetch_assoc($result)) {  
$res=array();  
$rows[] = $row;
 $post_parent=$row['ID'];  
 //echo '***************',$row['post_content'];
 }
 print json_encode($rows);
/
 ?>

Friday, 18 December 2015

TIME SAVING DATABASE FUNCTIONS

this post is from 
http://www.evoluted.net/thinktank/web-development/time-saving-database-functions

We’ve all been there, making an awesome form nicely laid out on the page and arranged pixel perfect to fit all the fields required; name, address, email, telephone etc… everything is going great until you come to the PHP.
Suddenly you realise that you have to process and add every item from this massive beautiful form in to the database! D’OH!
Normally you would have to create a SQL query similiar to the one below (assuming the variables had been sanitised first):
mysql_query(
"INSERT INTO my_table(first_name, last_name, email, address1, address2, address3, postcode, tel, mobile, website, contact_method, subject, message, how_you_found_us, time)
VALUES('$first_name', '$last_name', '$email', '$address1', '$address2', '$address3', '$postcode', '$tel', '$mobile', '$website', '$contact_method', '$subject', '$message', '$how_you_found_us', ".time().")
");
Thankfully there is an easier way, with some short hand database functions. Imagine being able to create the above query with just:
dbRowInsert('my_table', $form_data);
How much simpler is that? How about when updating a record or deleting one? How much easier would it be to have a simliar set of functions like:
dbRowUpdate('my_table', $form_data, "WHERE id = '$id'");
dbRowDelete('my_table', "WHERE id = '$id'");

HOW IT’S DONE

The key for these functions to work is the format for the $form_data variable. The variable is an array and should be arranged so the key of each element in the array is the column name for the data in the value part of the array. For the table described in the INSERT statement above, our array would look something similiar to this:
$form_data = array(
    'first_name' => $first_name,
    'last_name' => $last_name,
    'email' => $email,
    'address1' => $address1,
    'address2' => $address2,
    'address3' => $address3,
    'postcode' => $postcode,
    'tel' => $tel,
    'mobile' => $mobile,
    'website' => $website,
    'contact_method' => $contact_method,
    'subject' => $subject,
    'message' => $message,
    'how_you_found_us' => $how_you_found_us,
    'time' => time()
);
With the array formatted in this way it allows us to use the array_keys() php function to retrieve the field columns and then implode the array itself to build the centre part of the ‘insert’ query so we can then prepend and append the relevant data to construct the full query. Here is how we do it:
function dbRowInsert($table_name, $form_data)
{
    // retrieve the keys of the array (column titles)
    $fields = array_keys($form_data);

    // build the query
    $sql = "INSERT INTO ".$table_name."
    (`".implode('`,`', $fields)."`)
    VALUES('".implode("','", $form_data)."')";

    // run and return the query result resource
    return mysql_query($sql);
}
Simples! While this is totally functional, one thing to remember when doing this to make sure any data passed in to the function (be it table name or the actual form data) is passed through a sanitising function first. At the very minimum, data should be passed through mysql_real_escape_string() before going into a query, this helps to prevent SQL injections.

FURTHER FUNCTIONS

Now we have the insert query, what about the other types? Here is how we would create a delete function:
// the where clause is left optional incase the user wants to delete every row!
function dbRowDelete($table_name, $where_clause='')
{
    // check for optional where clause
    $whereSQL = '';
    if(!empty($where_clause))
    {
        // check to see if the 'where' keyword exists
        if(substr(strtoupper(trim($where_clause)), 0, 5) != 'WHERE')
        {
            // not found, add keyword
            $whereSQL = " WHERE ".$where_clause;
        } else
        {
            $whereSQL = " ".trim($where_clause);
        }
    }
    // build the query
    $sql = "DELETE FROM ".$table_name.$whereSQL;

    // run and return the query result resource
    return mysql_query($sql);
}
Now for an update function:
// again where clause is left optional
function dbRowUpdate($table_name, $form_data, $where_clause='')
{
    // check for optional where clause
    $whereSQL = '';
    if(!empty($where_clause))
    {
        // check to see if the 'where' keyword exists
        if(substr(strtoupper(trim($where_clause)), 0, 5) != 'WHERE')
        {
            // not found, add key word
            $whereSQL = " WHERE ".$where_clause;
        } else
        {
            $whereSQL = " ".trim($where_clause);
        }
    }
    // start the actual SQL statement
    $sql = "UPDATE ".$table_name." SET ";

    // loop and build the column /
    $sets = array();
    foreach($form_data as $column => $value)
    {
         $sets[] = "`".$column."` = '".$value."'";
    }
    $sql .= implode(', ', $sets);

    // append the where statement
    $sql .= $whereSQL;

    // run and return the query result
    return mysql_query($sql);
}

Wednesday, 1 July 2015

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Saturday, 3 January 2015

php code for Sending free SMS from WAY2SMS

<?php

send_sms(9880938687,'umeara',9487761900,'hi how ry ');

function send_sms($userID, $userPWD, $recerverNO, $message)
{
    if (!function_exists('curl_init')) {
        echo "Error : Curl library not installed";
        return FALSE;
    }
    $message_urlencode = rawurlencode($message);
    if (strlen($message) > 140) {
        $message = substr($message, 0, 139);
    }
   
    $cookie_file_path = "./cookie.txt";
    $temp_file        = "./temporary.txt";
    $user_agent       = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36";
   
    // LOGIN TO WAY2SMS
   
    $url        = "http://site24.way2sms.com/content/Login1.action";
    $parameters = array(
        "username" => "$userID",
        "password" => "$userPWD",
        "button" => "Login"
    );
   
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($parameters));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    $result = curl_exec($ch);
    curl_close($ch);
   
    // SAVE LOGOUT URL
   
    file_put_contents($temp_file, $result);
    $result     = "";
    $logout_url = "";
    $file       = fopen($temp_file, "r");
    $line       = "";
    $cond       = TRUE;
    while ($cond == TRUE) {
        $line = fgets($file);
        if ($line === FALSE) { // EOF
            $cond = FALSE;
        } else {
            $pos = strpos($line, ' window.location="');
            if ($pos === FALSE) {
                $line = "";
            } else { // URL FOUND
                $cond       = FALSE;
                $logout_url = substr($line, -25);
                $logout_url = substr($logout_url, 0, 21);
            }
        }
    }
    fclose($file);
   
    // SAVE SESSION ID
   
    $file = fopen($cookie_file_path, "r");
    $line = "";
    $cond = TRUE;
    while ($cond == TRUE) {
        $line = fgets($file);
        if ($line === FALSE) { // EOF
            $cond = FALSE;
        } else {
            $pos = strpos($line, "JSESSIONID");
            if ($pos === FALSE) {
                $line = "";
            } else { // SESSION ID FOUND
                $cond = FALSE;
                $id   = substr($line, $pos + 15);
            }
        }
    }
    fclose($file);
   
    // SEND SMS
   
    $url        = "http://site24.way2sms.com/smstoss.action?Token=" . $id;
    $parameters = array(
        "button" => "Send SMS",
        "mobile" => "$recerverNO",
        "message" => "$message"
    );
   
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($parameters));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    $result = curl_exec($ch);
    curl_close($ch);
   
    // LOGOUT WAY2SMS
   
    $url = "site24.way2sms.com/" . $logout_url;
   
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    $result = curl_exec($ch);
    curl_close($ch);
   
    // DELETE TEMP FILES
   
    unlink($cookie_file_path);
    unlink($temp_file);
   
    return TRUE;
   
}
?>

Friday, 4 April 2014

How to write SOAP web service using PHP?

    How to write web service using SOAP in PHP?
              
             In my previous post I had shown you “how to read gmail inbox using PHP”. Today I am writing my web service using SOAP. If you are wondering “How to create SOAP service?” , Then solution is here. Here I have createdwebservice. All you need to do is, you just have to call service and function to perform particular task.
So let’s follow steps to do this stuffs,

Step 1 : Download nusoap zip file from here.  And paste it inside your www folder if you are using wamp, for xampp user paste it inside htdocs/xampp folder.
Step 2:  create two file call server.php and client.php.
Step 3 :  Inside server.php write below lines of code.
<?php
require_once('lib/nusoap.php');
//require("Connection.class.php"); 
$server = new nusoap_server;
//$server ->configureWSDL('server', 'urn:server');
//$server ->wsdl->schemaTargetNamespace = 'urn:server';
//$server ->register('pollServer', array('value' => 'xsd:string'), array('return' => 'xsd:string'), 'urn:server', 'urn:server#pollServer');

//register a function that works on server
$server ->register('getfeedDetails', array('value' => 'xsd:string'), array('return' => 'xsd:string'), 'urn:server', 'urn:server# getfeedDetails');




// create the function to fetch Data’s from Database
function getfeedDetails ()
{
              $conn = mysql_connect('localhost','root','');
                mysql_select_db('news', $conn);
                $sql = "SELECT * FROM user_story";
                $q = mysql_query($sql);
                       $items = array();
                while($row = mysql_fetch_array($q)){

                                $items [] = array(
                                                        'story_url'=>$row['story_url'],
                                                        'story_title'=>$row['story_title'],
                                                        'story_description'=>$row['story_description'],
                                                        'story_image'=>$row['story_image']
                                    );
                }
                      return $items;

}

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server ->service($HTTP_RAW_POST_DATA);
?>
         That’s it you have created your server file to access and fetch details from database.
Step 4:    Now create client.php and inside it just  write below code. Create client object and call your server function to retrieve details.
<?php
require_once('lib/nusoap.php');
$url = "http://localhost/testserver/server.php?wsdl";
$client = new nusoap_client($url);
//Call server function
$response = $client ->call(' getfeedDetails ');

if($client->fault)
{
                echo "FAULT: <p>Code: (".$client ->faultcode.")</p>";
                echo "String: ".$client->faultstring;
}
else
{
                $result = $response;
                $count = count($result);
?>
    <table >
    <tr>
                <th>Story Url</th>
                <th>Story Title</th>
                <th>Story Description</th>
                <th>Story Image</th>
    </tr>
    <?php

    for($i = 0;$i < $count-1;$i++) {
                  $rowtype = ($i % 2) ? "style='background:#88DAEB'": "style=background:#FFF";
        ?>
    <tr <?php echo $rowtype; ?>>
                <td><?php echo $result[$i]['story_url']?></td>
                <td><?php echo $result[$i]['story_title']?></td>
                <td><?php echo $result[$i]['story_description']?></td>
                <td><img src="<?php echo $result[$i]['story_image']?>" type="photo"></td>
    </tr>
    <?php
                }
                ?>
    </table>
    <?php
}
 ?>
<style type="text/css">
    th {
        background:#007F99;
        color:#fff;
    }
</style>

?>
Step 5:   That’s it you have done. Now run your client.php in your browser. It will call server and fetch the details from database.  
So today I have shown you how to use simple SOAP webservice and to get details from database.