Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Oct 3, 2015

cURL

Not Curl.

cURL is a way you can hit a URL from your code to get a html response from it. its use for command line cURL from the php language.
php
//step1
$cSession = curl_init(); 
//step2
curl_setopt($cSession,CURLOPT_URL,"http://www.google.com/search?q=curl");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false); 
//step3
$result=curl_exec($cSession);
//step4
curl_close($cSession);
//step5
echo $result;
?> 
step1: Initialize a curl session use curl_init().
step2: Set option for CURLOPT_URL. This value is the URL which we are sending the request to. Append a search term "curl" using parameter "q=". Set option for CURLOPT_RETURNTRANSFER, true will tell curl to return the string instead of print it out. Set option for CURLOPT_HEADER, false will tell curl to ignore the header in the return value.
step3: Execute the curl session using curl_exec().
step4: Close the curl session we have created.
step5: Output the return string.
public function curlCall($apiurl, $auth, $rflag)
{
    $ch = curl_init($apiurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    if($auth == 'auth') { 
        curl_setopt($ch, CURLOPT_USERPWD, "passw:passw");
    } else {
        curl_setopt($ch, CURLOPT_USERPWD, "ss:ss1");
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $dt = curl_exec($ch);        
    curl_close($ch);
    if($rflag != 1) {
        $dt = json_decode($dt,true);        
    }
    return $dt;
}
this is also use for authentication.and we can also set the user name password for authentication for function user you can see the
http://php.net/manual/en/ref.curl.php
http://www.startutorial.com/articles/view/php-curl

Sep 23, 2015

Generate random string with PHP

If you want to generate random code with PHP you can use this function:

<?php
public function genRandmStr($lgt = 8){
       
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@#!';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $lgt; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
}

// you can see the result here
echo genRandmStr();
?>

Sep 18, 2015

CakePHP how to retrieve POST and GET vars

When you are working with CakePHP framework and you try to retrieve the variables which have been sent through GET or POST.

For POST variables you have

For $_POST['key']  we have
$this->request->data('key');


For GET variables you have

For $_GET['kpa']  we have
$this->request->query('kpa');


Aug 28, 2015

CakePHP and PHP version

I was trying to code at my project. It was OK on my localhost webserver. But... when I moved the files on the real webserver I had a problem.
I didn't know what could that be. I got errors just on some pages. Not at all pages of my website.
Then, I realized that, perhaps, my version of PHP could be too old. Actually, not my version but real webserver version.

Indeed, the problem it was with PHP version which is 5.3 and this version doesn't know about the arrays declared like this:

$ar = ['key1' => 'value1', 'key2' => 'value2'];

It knows only like this:

$ar = array('key1' => 'value1', 'key2' => 'value2');

I got this issue when I tried to make a Form and I finished to have this PHP code

echo $this->Form->input('name', [
                'label'=>[
                    'class' =>  'ContactInput',
                    'text'  =>  'Name:' . $messgError['name']
                ]

            ]);



Aug 26, 2015

CakePHP 2 - Forms

When you try to create a new form in CakePHP framework using FormHelper, you may have this error:

Missing Database  Table
And if you read very carefully  the Book of CakePHP will see something like that:

You can also pass false for $model. This will place your form data into the array: $this->request->data (instead of in the sub-array: $this->request->data['Model']). This can be handy for short forms that may not represent anything in your database.
So, in your view file like APP/View/Contact/index.ctp, you have  to start with this line:

echo $this->Form->create(false, array('url' => '/contact'));

Good luck!

Aug 19, 2015

CakePHP 2.* how to use custom complex sql Query with model

Let's suppose that we have MySQL database with table categories:

CREATE TABLE `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category` varchar(64) DEFAULT NULL,
  `description` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;


Then, in CakePHP framework, you want to call a simple query as you did it with mysql_query in PHP with no framework.

For that you have the class Model::query()

First, you build the model. You don't want to use predefined Models for a table (like the model Categories for DB table Categories). Because you want to use perhaps this query for extracting data from two tables which are related.

So,  first you have to create the model. For this you have to create the file APP/Model/Categs.php



class Categ extends AppModel {

    public $useTable = false; // it is mandatory to have this line. Whithout this line, 
                              //                                    the Controller will search for table Categ; it doesn't matter that you have a sql query  

    public function getDataFromMyTable(){
        $d = $this->query("SELECT * FROM  categories");
        return $d;
    }

}


Second, you have to create the Controller. The file will be: APP/Controller/CategsController.php


class CostsController extends AppController {
    
    
    public function index(){
        $this->loadModel('Categ');
    
        $rezQuery = $this->Categ->getDataFromMyTable();
        $this->set('cs', $rezQuery);
    }
}
?>


And now we have to create the view. We create folder APP/View/Categs (if this folder doesn't exists). And then we create the file index.ctp into this folder. The name of the ctp file should be the  same as name of the function from Controller. In index.ctp we write:


Aug 14, 2015

CakePHP and .ctp files

If you use CakePHP as PHP framework then you'll see some different types of files: CTP. And you'll need to see them as a php file in your PHP editor, Eclipse. For this you'll do this:

Preference>General>Content Types>Php then add *.ctp

And then you need to restart Eclipse IDE for have an effect.



CakePHP

Last 2 weeks I tried to put ZF2 to my project www.mateonline.net. It was endless pain to configure Zend Framework 2 for me as a beginner with this. ZF2 as syntax is very nice. But something got wrong with server configuration.
So, then I said I will try Symfony 2. But, I quit immediately. I though it will be easier, but it is no so nice as ZF2.
And, then I pass to CakePHP. Which I like. Except the fact that it has no PDO. I don't like  how is linked to MySQL DB but I can use to it.

Aug 3, 2015

PHP namespace

When you use namespace on PHP scripts you have to use require(), require_once(), include() or include_once() . It doesn't work without one of these like you can see on this example:

php namespaces are case-insensitive:

file1.php:
namespace foo;

function 
bar()
{
    echo 
'from bar';
}
?>
file2.php:


fOo
\bar() // output: from bar
?>
This example is here:
http://php.net/manual/en/language.namespaces.rationale.php

A better example is here:
http://www.sitepoint.com/php-53-namespaces-basics/

Oct 31, 2012

New forum

Starting today, the coders has a new forum for their comunnication. Here we are:
coderstalk.co.cc

It is structurated by software domains: PHP, ASP, .NET, C#, Databases and HTML&CSS.
We are 4 people which will respond for every question.

The website is based on the SMF Simple Machines Forum.

Oct 22, 2012

New project


www.pasainternational.ro
This is my last project which is almost done: www.pasainternational.ro

For the first page and offers pages I have implemented a cahe system based on the json files.

The Cache is available for 2 hours.

Oct 19, 2012

Caching

What is the best for caching:
Memcache or Redis?


Sep 18, 2012

Wordpress to regular website

Many clients ask me to build their websites with wordpress. So, now i found an interesting article about that:

"I get that for most of us entrepreneurs, especially when we're just getting started, we're bootstrapping and funds are tight.  That's what you need to jump in and use WordPress to build your website.  So, here's how to make a "regular" (non-blog) website using WordPress.  To see a larger version of any image below, just click on it.
Over the next few days I'll be posting about WordPress and how to get the most out of it.  But back to setting up your site…
First, you'll need to install WordPress on your hosting space.  This is not as scary as it sounds – really, it's not!  Most website hosts will even install it for free for you if you open a support ticket and ask.  Here's a video showing how to install WordPress using Fantastico (will open in a new window so you don't lose this post)."

Read more...

Apr 18, 2012

Good Day

It's so many days from when I wrote last post here on this blog. Now it's time to come back.
And I have read a nice article about PHP polymorphism:


Polymorphism is derived from two Greek words. Poly (meaning many) and morph (meaning forms).

Polymorphism means many forms. In C you have two methods with the same name that have different function signatures and hence by passing the correct function signature you can invoke the correct method.

This is how polymorphism is achieved in languages like C where in a function sum(int, int) differs from sum(float, float). Therefore the method sum() has many forms depending on the parameters being passed to it.

The meaning with Object Oriented languages changes. With Object Oriented language polymorphism happens:


When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism


Read the entire article here: http://widwebway.com/en/blog/?p=32

Mar 13, 2012

MVC PHP

I just use a new MVC. I have choose KISSMVC. Is not the best MVC but is one of the simpliest.

Feb 28, 2012

New project

Today, we lance a new project. It is BETA version. But the website is Up. It's about http://www.dentaldirectorylinks.com. It is a link directory with/for dentists and dental network in the world.

It's all withh html, css javascript, jQuery, Mysql and PHP.

Jan 26, 2012

The best Rich Text Editors to use in web CMS

I found an nice article about Rich Text Editors to use in HTML with PHP. It's about Yahoo! UI Library: Rich Text Editor, FCKeditors and others.


The article is not so newest but it's helpfuly: http://woork.blogspot.com/2009/02/best-rich-text-editors-ready-to-use-in.html



Dec 20, 2011

Creating XML from MySQL as easy as PI


ANALYSIS
Unfortunately, importing XML into MySQL is more like chocolate cheesecake, but there are solutions available. Just follow this guide and you'll be on your way to integrating Web services with your database in no time. Starting from scratch
With the growing popularity of XML, developers have found an easy method to present data sets in a standardised way. What else does that sound like? A database! It's only natural that it should be simple to convert your information without a lot of fuss -- and you can. Some proprietary database manufacturers, such as Microsoft and IBM, have taken steps to integrate XML into their systems. This comes as no surprise since these two companies are both heavily involved in the XML standardisation project. Not wanting to be left behind, the creators of MySQL database incorporated a means for generating an XML data file. It's supported in version 3.23.48 and up. You can use the command line or facilitate the process with the programming language of your choice. To get started, you can download MySQL database  for free from MySQL.com. The current release is sufficient to support this feature, and you don't need to compile it with any special parameters. Fruit filling
Once you're installed, created, and populated your database, execute the following command to generate an XML file:
mysqldump --xml databasename [tables]
If you'd like to save this to a file, simply use the standard *NIX method of outputting to a file:
mysqldump --xml databasename > filename.xml
This produces a well formed XML document. Because XML is datacentric, if you dump your entire database and it contains no information, your file will result in a series of empty tags based on the table names. Your output should look something like this:

Now you're free to use this data file with any application you desire. This method is useful in a number of ways. Not only will it create a standardised representation of your data, but it can also take a snapshot of your database (or portion of your database) for display. Rather than making repeated calls to the database server, just generate an XML document when your database changes and reference that from Web pages or whatever you're using. This can localize calls for data, reduce the overhead of frequent calls to a database, and easily present a subset of your information for improved performance, security, or localization. Ice cream on the side
It's really easy to get XML from MySQL, but how about the other direction? That's a little trickier. MySQL itself doesn't support this function, and with good reason. The database currently has no way to validate the XML file. This could result in a number of scenarios, ranging from a partial load to ignoring malformed tags and statements to simply forcing the entire load to fail. MySQL supports only cascading back-outs in current development versions. While it's not pretty from a native standpoint, you do have some options. One solution is Perl's DBIx::XML_RDB module. You can use this method to both import and export XML, though understandably the import is heavily dependent upon a correctly structured XML file. To get the data, the module essentially runs a query and formats the results in an XML file. Conversely, you can use the module to read an XML file, create a SQL query, and execute it. There is a simpler option as well. The DBIx::XML_RDB module ships with two utility scripts to facilitate the process: xml2sql.pl and sql2xml.pl. I found agreat tutorial  on using this Perl module at O'Reilly's XML.com Web site. It will walk you through the process. Another, more generalized effort comes from Ron Bourret's XML-DBMS project . This is an ongoing effort to support XML imports and exports with relational databases using Perl and Java. There's also some very interesting work that supports mapping one database to another using XML as facilitating middleware. This is a community-oriented open source project being managed on SourceForge . Other languages, particularly Web scripting languages, haven't ignored the need to import XML into SQL databases either. There are similar efforts for Python, such as the xml2sql and dtd2sql modules, outlined in detail in this article from IBM , and a couple of projects in the works for PHP, such as the "XML MySQL class" project. Scrumptious
With these utilities, importing and exporting XML into and from MySQL is easy! Since MySQL is popular and free, it's been the test bed for integrating many scripting languages in XML, and as a result there are a number of tutorials and scripts specific for this database. With the power of a relational database and the ability to easily create XML files, MySQL can be an integral part of your Web services solution.

Nov 25, 2011

Learning PHP


PHP is the world's most popular web development language. Started by Danish-Greenlandic programmer Rasmus Lerdorf in 1995 it is now installed on more than 20 million websites and 1 million web servers and counting. 


It is estimated that for every 100 PHP developers, there are 42 Perl developers, 12 Python developers and 4 Ruby developers - PHPs popularity is the central reason why you should consider learning it above all others. 


PHP is the basis of Content Management Systems such as Drupal, Joomla and WordPress so gaining a knowledge of PHP would help you in using these scripts. 


Presumably your are already proficient with CSS and HTML and want to take your web creativity to another level. If you aren't, then stop right here. It's unthinkable to tackle PHP without a firm grounding in HTML and a good knowledge of CSS would be extremely useful. 


You don't have to have a complete knowledge of HTML in order to learn PHP but you certainly need to know the basics - the rest you will pick up in tandem with PHP. For instance, if you use Content Management Systems all the time you'll unlikely to be that familiar with coding forms, but HTML forms are an essential part of PHP and you'll need to be able to create them quickly and without fuss. 


Learning PHP is as hard as you can imagine it to be. You need time and lots of patience and preferably a reality you need to escape from for an inordinate amount of time. It's a good idea to pace yourself and set a two year framework in order to become familiar with the core of the language.


An interesting article here.