domingo, 31 de diciembre de 2017

php printing a date object


<?php
$date=date_create("2013-03-15");
echo date_format($date,"Y/m/d H:i:s");
?>


https://www.w3schools.com/php/func_date_date_format.asp

lunes, 25 de diciembre de 2017

php jquery pagination

https://datatables.net/

http://www.kodingmadesimple.com/2017/12/jquery-datatables-php-mysql-ajax.html?utm_content=buffer1b30d&utm_medium=social&utm_source=facebook.com&utm_campaign=buffer

sábado, 23 de diciembre de 2017

php curl example

<?php $curl = curl_init(); curl_setopt ($curl, CURLOPT_URL, "http://www.php.net"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec ($curl); curl_close ($curl); print $result; ?>



<?php $curl = curl_init(); curl_setopt($curl, CURLOPT_URL,"http://localhost/posttest.php"); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, "Hello=World&Foo=Bar&Baz=Wombat"); curl_exec ($curl); curl_close ($curl); ?>


http://php.net/manual/en/function.curl-setopt.php

http://www.hackingwithphp.com/15/10/2/your-first-curl-scripts



jueves, 21 de diciembre de 2017

twilio response to sms

<?php
$number = $_POST['From'];
$body = $_POST['Body'];

header('Content-Type: text/xml');
?>

<Response>
    <Message>
        Hello <?php echo $number ?>.
        You said <?php echo $body ?>
    </Message>
</Response>


https://www.twilio.com/blog/2016/08/receive-sms-php-twilio.html

Setup Twilio Phone Number

Sign up for a free Twilio account if you don’t have one.
Buy a phone number, then click Setup Number. Scroll to the Messaging section and find the line that says “A Message Comes In.”
message-comes-in
Fill in the full path to your file (i.e., https://yourserver.com/message.php) and click Save.
Now, send an SMS to your shiny new phone number number and revel in the customized response that comes back your way.

miércoles, 20 de diciembre de 2017

php xml

<!DOCTYPE html>
<html>
<body>

<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;

$xml=simplexml_load_string($note);
echo $xml->getName() . "<br>";
 
foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br>";
  }
?>
 

</body>
</html>

Tove
Jani
Reminder
Don't forget me this weekend!

------------------------------------------------



<!DOCTYPE html>
<html>
<body>

<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;

$xml=simplexml_load_string($note);

echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
 

</body>
</html>


Tove
Jani
Reminder
Don't forget me this weekend!


https://www.w3schools.com/PhP/func_simplexml_load_string.asp

jueves, 14 de diciembre de 2017

php loading xml into array

<?php
$xmlstring=file_get_contents("http://180.110.13.81/default/en_US/send_sms_status.xml");
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
print_r($array);
echo " $array[smskey1] $array[status1] $array[error1]";
?>



This XML file does not appear to have any style information associated with it. The document tree is shown below.
<send-sms-status>
<smskey1>697409980</smskey1>
<status1>DONE</status1>
<error1/>
<smskey2/>
<status2/>
<error2/>
<smskey3/>
<status3/>
<error3/>
<smskey4/>
<status4/>
<error4/>
<smskey5/>
<status5/>
<error5/>
<smskey6/>
<status6/>
<error6/>
<smskey7/>
<status7/>
<error7/>
<smskey8/>
<status8/>
<error8/>
</send-sms-status>

php .=

<?php
$rand="18097143489_";
$rand .= rand();

echo  $rand;
?>

18097143489_1693751741

viernes, 8 de diciembre de 2017

creating a csv with dowload

<?php

$csv_file = "csv_export_".date('Ymd') . ".csv";

header("Content-Type: text/csv");

header("Content-Disposition: attachment; filename=\"$csv_file\"");


$out = fopen('php://output', 'w');

$list=array('Name','LastName', 'Age' 'Address', 'state');

fputcsv($out, $list);


fclose($out);
?>


http://php.net/manual/en/function.fputcsv.php


http://www.phpzag.com/create-a-csv-file-using-phpmysql/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+phpzag+%28PHP+Tutorial%29

writing to a csv with fputcsv

<?php
$list = array
(
"Peter,Griffin,Oslo,Norway",
"Glenn,Quagmire,Oslo,Norway",
);

$file = fopen("contacts.csv","w");

foreach ($list as $line)
  {
  fputcsv($file,explode(',',$line));
  }

fclose($file); ?>
The CSV file will look like this after the code above has been executed:
Peter,Griffin,Oslo,Norway
Glenn,Quagmire,Oslo,Norway

sábado, 2 de diciembre de 2017

google translation API

composer require google/cloud-translate
<?php
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
use Google\Cloud\Translate\TranslateClient;

# Your Google Cloud Platform project ID
$projectId = 'voice-184406';


# Your Google login information
putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/html/google_api/voice.json'); # Instantiates a client $translate = new TranslateClient([ 'projectId' => $projectId ]); ####variables#### $text=$argv['1']; $lang=$argv['2']; # The text to translate # The target language $target = "$lang"; # Translates some text into spanish $translation = $translate->translate($text, [ 'target' => $target ]); echo 'Text: ' . $text . ' Translation: ' . $translation['text']; ?>

php translator.php  " Hola  gracias por leer este articulo" en
Text:  Hola  gracias por leer este articulo
Translation: Hi, thanks for reading this article

https://cloud.google.com/translate/docs/reference/libraries
https://cloud.google.com/translate/docs/languages

lunes, 20 de noviembre de 2017

adding slashes to qoutes




addslashes  Quote string with slashes
$str="asterisk -x ' sip show peers' ";

echo addslashes($str);


Output : asterisk -x \' sip show peers\'

domingo, 5 de noviembre de 2017

Open directory and its contents

<?php
$dir 
"/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if (
$dh opendir($dir)) {
        while ((
$file readdir($dh)) !== false) {
            echo 
"filename: $file : filetype: " filetype($dir $file) . "\n";
        }
        
closedir($dh);
    }
}
?>

filename: . : filetype: dir
filename: .. : filetype: dir
filename: apache : filetype: dir
filename: cgi : filetype: dir
filename: cli : filetype: dir


sábado, 28 de octubre de 2017

Install and Use PHP Composer on Ubuntu 16.04


Table of Contents

Introduction

PHP Composer is a package management system for PHP which prevents users from having to "reinvent the wheel" when it comes to commonly-used website components like user authentication or database management. Composer is modeled on other popular package management systems like Ruby's Bundler.
Note: For any 1&1 Cloud Server with Plesk, applications like PHP Composer should always be installed and managed through the Plesk interface.

Composer vs PEAR

PEAR was the first substantial package management system for PHP. However, PEAR has fallen out of favor with developers in recent years.
Due to the difficult process of getting packages approved for inclusion with PEAR, many of the packages available through PEAR are out of date. PEAR also requires users to install packages system-wide, whereas Composer allows you to install packages either system-wide or on a per-project basis.
Composer also tends to be better at handling dependencies, has a wider and more up-to-date codebase, and is more actively maintained.

Requirements

  • A 1&1 Cloud Server running Linux (Ubuntu 16.04)
    • PHP installed and configured, version 5.3.2 or higher
Use the command php -v to check your PHP version:
user@localhost:~# php -v
PHP 7.0.13-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.13-0ubuntu0.16.04.1, Copyright (c) 1999-2016, by Zend Technologies
In this example, the server is running PHP version 7.0.13.

Install PHP Composer

Update your packages:
sudo apt-get update
Install the curl utility:
sudo apt-get install curl
Download the installer:
sudo curl -s https://getcomposer.org/installer | php
Move the composer.phar file:
sudo mv composer.phar /usr/local/bin/composer
Use the composer command to test the installation. If Composer is installed correctly, the server will respond with a long list of help information and commands:
user@localhost:~# composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                /_/
Composer version 1.3.2 2017-01-27 18:23:41

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message

Using PHP Composer

To use Composer, you will create a composer.json file in your project directory, then use the php composer.phar install command to install the required dependencies.
The composer.json file specifies which packages you want Composer to install and manage. To find available packages, visit the main PHP Composer repository which aggregates all of the public PHP packages that can be installed using Composer.
For this tutorial, we will install the PHP framework Symfony, which is used by thousands of projects, including Spotify, Drupal, and Magento.
Here is the Symfony page on Packagist.org. We need two things from this page:
  1. The install command (composer require symfony/symfony)
  2. The current version (3.2.4)
Create a directory on your server for this project:
sudo mkdir /var/www/html/symfony-test
Move to that directory:
cd /var/www/html/symfony-test
Then create the composer.json file and open it for editing:
sudo nano composer.json
Put the following content into this file:
{
  "require": {
    "symfony/symfony": "3.2.4"
  }
}
Save and exit the file. Then use the following command to install Symfony:
composer install
This command will check the composer.json file and download and install anything specified there. After Composer finishes the installation, you can use ls -la to verify that Composer has created a vendor directory, and that Symfony has been installed in this directory:
Content provided by 1&1

domingo, 8 de octubre de 2017

add spaces between character

<?php
 $password="1bsdf4";
 echo chunk_split($password, 1, ' ');
chunk_split() is build-in PHP function for splitting string into smaller chunks.

viernes, 22 de septiembre de 2017

php get mp3 file duation

<?php
 $duration=shell_exec("mp3info -p \"%m:%s\n\" $argv[1]");
echo $duration;

?>


 php mp3info.php /var/www/html/dialer/audio/57c081f2de8cd.mp3
0:34
Duration in minutes en seconds


miércoles, 20 de septiembre de 2017

php filters

if (filter_input(INPUT_GET, "num", FILTER_VALIDATE_INT)) {


}


  • Filter Functions
    • filter_has_var — Checks if variable of specified type exists
    • filter_id — Returns the filter ID belonging to a named filter
    • filter_input_array — Gets external variables and optionally filters them
    • filter_input — Gets a specific external variable by name and optionally filters it
    • filter_list — Returns a list of all supported filters
    • filter_var_array — Gets multiple variables and optionally filters them
    • filter_var — Filters a variable with a specified filter
  • http://php.net/manual/en/filter.filters.validate.php
    [0] => int
    [1] => boolean
    [2] => float
    [3] => validate_regexp
    [4] => validate_url
    [5] => validate_email
    [6] => validate_ip
    [7] => string
    [8] => stripped
    [9] => encoded
    [10] => special_chars
    [11] => unsafe_raw
    [12] => email
    [13] => url
    [14] => number_int
    [15] => number_float
    [16] => magic_quotes
    [17] => callback
)
add a note

sábado, 16 de septiembre de 2017

php converting from second to minutes

<?php

$time=9;

if($time>60){
$min=($time/60);

if(is_float($min)){

$min=floor($min);
        $min=($min*60);

$sec=($time-$min);
        $min=($min/60);

        echo " $min:$sec Min";


}


else {
echo "$time Min";
}


}

else {
echo "$time Sec";

}
?>


Function mode

<?php

function minutes($time) {

if($time>60){
$min=($time/60);

if(is_float($min)){

$min=floor($min);
        $min=($min*60);

$sec=($time-$min);
        $min=($min/60);

        return " $min:$sec Min";


}


else {

return "$time Min";
}


}

else {
return "$time Sec";

}

}

?>

echo minute(784);

miércoles, 13 de septiembre de 2017

php download file

<?php
$fname=uniqid();
$attachment_location ="/var/spool/asterisk/monitor/OUT102-20170829-085404-1504022044.824.wav";
        if (file_exists($attachment_location)) {
            header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
            header("Cache-Control: public"); // needed for internet explorer
            header("Content-Type: application/wav");
            header("Content-Transfer-Encoding: Binary");
            header("Content-Length:".filesize($attachment_location));
            header("Content-Disposition: attachment; filename=$fname.wav");
            readfile($attachment_location);
            die();
        } else {
            die("Error: File not found.");
        }

?>

miércoles, 6 de septiembre de 2017

STDIN & STDOUT


<?php fwrite(STDOUT, "Please enter your namen");

// Read the input
$name = fgets(STDIN);



fwrite(STDOUT, "Hello $name");

// Exit correctly
exit(0);
?>



echo " hello world " | php stdin.php
result  hello world

<?php
//$stdin = fopen('php://stdin', 'r');
$stdin=stream_get_contents(STDIN);
echo $stdin;
?>

Other method

<?php

echo  " This is the result $argv[1]";

?>

a=$(echo 'ambiorix') | php cmd.php $a
 This is the result ambiorix


viernes, 25 de agosto de 2017

print date utc and convert date to time stamp

<?php
print gmdate("Y-m-d\TH:i:s\Z");

$utc=gmdate("Y-m-d\TH:i:s\Z");
echo "<br>";

echo strtotime($utc);

echo "<br>";

$fromunixtime_utc=gmdate("Y-m-d\TH:i:s\Z",strtotime($utc));

echo $fromunixtime_utc;

?>

2017-08-26T02:52:01Z
1503715921

viernes, 18 de agosto de 2017

returning as json content type

<?PHP
$data = /** whatever you're serializing **/;
header('Content-Type: application/json');
echo json_encode($data);

https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script