Thursday, October 17, 2013

installed Xdebug on a Fedora 17 OS

I installed Xdebug on a Fedora 17 OS in order to use it with netbeans 7.3.1 It took me some time so I share with you how did I managed to have it working:
Install Xdebug:
sudo yum install php-pecl-xdebug
Add this lines to php.ini at the end of the file:
sudo vi /etc/php.ini
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000

Add the same lines to xdebug.ini file:
sudo vi /etc/php-zts.d/xdebug.ini
xdebug.remote_enable=1 xdebug.remote_handler=dbgp xdebug.remote_mode=req xdebug.remote_host=127.0.0.1 xdebug.remote_port=9000
Allow port for Xdebug
sudo semanage port -a -t http_port_t -p tcp 9000
Now find your netbeans.conf file in your system:
sudo find / -name netbeans.conf
Now, open up the file and add the text "-J-Dorg.netbeans.modules.php.dbgp.level=400" in netbeans_default_options so that the line looks somewhat like this:
netbeans_default_options="-J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-Dnetbeans.logger.console=true -J-ea -J-Dapple.laf.useScreenMenuBar=true -J-Dapple.awt.graphics.UseQuartz=true -J-Dsun.java2d.noddraw=true -J-Dsun.zip.disableMemoryMapping=true -J-Dsun.awt.disableMixing=true -J-Dnetbeans.extbrowser.manual_chrome_plugin_install=yes -J-Dorg.netbeans.modules.php.dbgp.level=400"
Restart both NetBeans IDE and apache:
sudo service httpd restart
if not working then use

SQLSTATE[HY000]: General error: 1 Can't create/write to file '/tmp

You messed up with /tmp permissions. To fix:
chown root:root /tmp (root:root is mysql username and password which you used)
chmod 1777 /tmp 
/etc/init.d/mysqld restart (mysqld service restart)

Tuesday, September 10, 2013

[SOLVED] How to fix filesystem error - Fedora

/dev/mapper/volgroup-lv_root contains a filesystem with errors checks forced

run below command

# /dev/mapper/volgroup-lv_root
# fsck -f  /dev/mapper/volgroup-lv_root

Thursday, September 5, 2013

MySql backup with php script

<?php

    $link = mysql_connect($host,$user,$pass);
    mysql_select_db($database_name,$link);

    $tableName  = '(select * from table_name where date(created_at) = "2013-09-04")';
  //$tableName = 'table_name';
    $backupFile = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'backup.sql';

    $query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";

    $result = mysql_query($query);

    echo "backup complete";

    mysql_close($link);
    $link = null;

?>

Restore table script

<?php
 

    $link = mysql_connect($host,$user,$pass);
     mysql_select_db($database_name,$link);

    $tableName  = 'table_name';
    $backupFile = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'backup.sql';

    $query      = "LOAD DATA INFILE '$backupFile' INTO TABLE $tableName";
    $result = mysql_query($query);

    echo "backup restore complete";

    mysql_close($link);
    $link = null;

?>

Monday, August 12, 2013

X-Debug in php 5.4.17


I installed Xdebug on a Fedora 17 OS in order to use it with netbeans 7.3.1 It took me some time so I share with you how did I managed to have it working:

Install Xdebug:

sudo yum install php-pecl-xdebug

Add this lines to php.ini at the end of the file:

sudo vi /etc/php.ini

zend_extension=/usr/lib64/php/modules/xdebug.so

xdebug.remote_enable=1

xdebug.remote_handler=dbgp

xdebug.remote_mode=req

xdebug.remote_host=127.0.0.1

xdebug.remote_port=9000

Add the same lines to xdebug.ini file:

sudo vi /etc/php-zts.d/xdebug.ini


xdebug.remote_enable=1 xdebug.remote_handler=dbgp xdebug.remote_mode=req xdebug.remote_host=127.0.0.1 xdebug.remote_port=9000

Allow port for Xdebug

sudo semanage port -a -t http_port_t -p tcp 9000

Now find your netbeans.conf file in your system:

sudo find / -name netbeans.conf


Now, open up the file and add the text "-J-Dorg.netbeans.modules.php.dbgp.level=400" in netbeans_default_options so that the line looks somewhat like this:


netbeans_default_options="-J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-Dnetbeans.logger.console=true -J-ea -J-Dapple.laf.useScreenMenuBar=true -J-Dapple.awt.graphics.UseQuartz=true -J-Dsun.java2d.noddraw=true -J-Dsun.zip.disableMemoryMapping=true -J-Dsun.awt.disableMixing=true -J-Dnetbeans.extbrowser.manual_chrome_plugin_install=yes -J-Dorg.netbeans.modules.php.dbgp.level=400"


Restart both NetBeans IDE and apache:

sudo service httpd restart

if not working then use

xdebug.remote_host=127.0.0.1 to ip address of computer

Saturday, March 23, 2013

how to install mongo db

Install mongo db step in centos/fedora

first check your os (32/64)
uname -m

Create a /etc/yum.repos.d/10gen.rep

open file and add bellow line in file and save the file (64bit os)
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
enabled=1

open file and add bellow line in file and save the file (62bit os)
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686
gpgcheck=0
enabled=1

~]# yum install mongo-10gen mongo-10gen-server

~]# service mongo db start

~]# chkconfig --levels 235 mongod on

// step for install php mongo drive

~]# yum install php-devel git httpd

~]# export PHP_AUTOCONF=/usr/bin/autoconf

~]# export PHP_AUTOHEADER=/usr/bin/autoheader

~]# wget http://pecl.php.net/get/mongo-1.0.9.tgz

~]# tar -xzf mongo-1.0.9.tgz

~]# cd mongo-1.0.9

~]# phpize

~]# ./configure

~]# make install

~]# cd /etc/php.d

~]# vi mongo.ini

add bellow line in mongo.ini
extension=mongo.so

~]# service httpd restart

//test mongo db in prompt
//strat mongo
~]# mongo

//databse test use
>use test;

>doc = {"hello": "world", "php": "is alive!" };
>db.phptest.save(doc);
>db.phptest.findOne();

Example in php

<?php
$connection = new Mongo();
$db = $connection->test;
$collection = $db->phptest;

$obj = $collection->findOne();
echo "Hello " . $obj["hello"] . "!";

echo "Show result as an array:";
echo "<pre>";
print_r($obj);
echo "</pre>";

echo "Show result as JSON:";
echo "<pre>";
echo json_encode($obj);
echo "</pre>";


?>

install rockmongo for web view

download from rockmongo.com/downloads

extract file in web directory

create virtualhost


Friday, February 8, 2013

เคธเคฐเคซ़เคฐोเคถी เค•ी เคคเคฎเคจ्เคจा (Sarfaroshi ki Tamanna)

เคธเคฐเคซ़เคฐोเคถी เค•ी เคคเคฎเคจ्เคจा (Sarfaroshi ki Tamanna)

เคธเคฐเคซ़เคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै
เคฆेเค–เคจा เคนै เคœ़ोเคฐ เค•िเคคเคจा เคฌाเคœ़ू-เค-เค•़ाเคคिเคฒ เคฎें เคนै

เค เคตเคคเคจ, เค•เคฐเคคा เคจเคนीं เค•्เคฏूँ เคฆूเคธเคฐी เค•ुเค› เคฌाเคคเคšीเคค,
เคฆेเค–เคคा เคนूँ เคฎैं เคœिเคธे เคตो เคšुเคช เคคेเคฐी เคฎเคนเคซ़िเคฒ เคฎें เคนै
เค เคถเคนीเคฆ-เค-เคฎुเคฒ्เค•-เค“-เคฎिเคฒ्เคฒเคค, เคฎैं เคคेเคฐे เคŠเคชเคฐ เคจिเคธाเคฐ,
เค…เคฌ เคคेเคฐी เคนिเคฎ्เคฎเคค เค•ा เคšเคฐเคšा เค—़ैเคฐ เค•ी เคฎเคนเคซ़िเคฒ เคฎें เคนै
เคธเคฐเคซ़เคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै

เคตเค•़्เคค เค†เคจे เคชเคฐ เคฌเคคा เคฆेंเค—े เคคुเคे, เค เค†เคธเคฎाเคจ,
เคนเคฎ เค…เคญी เคธे เค•्เคฏा เคฌเคคाเคँ เค•्เคฏा เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै
เค–ेँเคš เค•เคฐ เคฒाเคˆ เคนै เคธเคฌ เค•ो เค•़เคค्เคฒ เคนोเคจे เค•ी เค‰เคฎीเคฆ,
เค†เคถिเค•ों เค•ा เค†เคœ เคœเคฎเค˜เคŸ เค•ूเคšा-เค-เค•़ाเคคिเคฒ เคฎें เคนै
เคธเคฐเคซ़เคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै

เคนै เคฒिเค เคนเคฅिเคฏाเคฐ เคฆुเคถ्เคฎเคจ เคคाเค• เคฎें เคฌैเค ा เค‰เคงเคฐ,
เค”เคฐ เคนเคฎ เคคैเคฏाเคฐ เคนैं เคธीเคจा เคฒिเค เค…เคชเคจा เค‡เคงเคฐ.
เค–़ूเคจ เคธे เค–ेเคฒेंเค—े เคนोเคฒी เค…เค—เคฐ เคตเคคเคจ เคฎुเคถ्เค•़िเคฒ เคฎें เคนै
เคธเคฐเคซ़เคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै

เคนाเคฅ, เคœिเคจ เคฎें เคนै เคœूเคจूเคจ, เค•เคŸเคคे เคจเคนी เคคเคฒเคตाเคฐ เคธे,
เคธเคฐ เคœो เค‰เค  เคœाเคคे เคนैं เคตो เคुเค•เคคे เคจเคนीं เคฒเคฒเค•ाเคฐ เคธे.
เค”เคฐ เคญเคก़เค•ेเค—ा เคœो เคถोเคฒा เคธा เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै,
เคธเคฐเคซ़เคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै

เคนเคฎ เคคो เค˜เคฐ เคธे เคนी เคฅे เคจिเค•เคฒे เคฌाँเคงเค•เคฐ เคธเคฐ เคชเคฐ เค•เคซ़เคจ,
เคœाँ เคนเคฅेเคฒी เคชเคฐ เคฒिเค เคฒो เคฌเคข เคšเคฒे เคนैं เคฏे เค•เคฆเคฎ.
เคœ़िंเคฆเค—ी เคคो เค…เคชเคจी เคฎॆเคนเคฎाँ เคฎौเคค เค•ी เคฎเคนเคซ़िเคฒ เคฎें เคนै
เคธเคฐเคซ़เคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै

เคฏूँ เค–เคก़ा เคฎเค•़्เคคเคฒ เคฎें เค•़ाเคคिเคฒ เค•เคน เคฐเคนा เคนै เคฌाเคฐ-เคฌाเคฐ,
เค•्เคฏा เคคเคฎเคจ्เคจा-เค-เคถเคนाเคฆเคค เคญी เค•िเคธी เค•े เคฆिเคฒ เคฎें เคนै?
เคฆिเคฒ เคฎें เคคूเคซ़ाเคจों เค•ी เคŸोเคฒी เค”เคฐ เคจเคธों เคฎें เค‡เคจ्เค•เคฒाเคฌ,
เคนोเคถ เคฆुเคถ्เคฎเคจ เค•े เค‰เคก़ा เคฆेंเค—े เคนเคฎें เคฐोเค•ो เคจ เค†เคœ.
เคฆूเคฐ เคฐเคน เคชाเค เคœो เคนเคฎเคธे เคฆเคฎ เค•เคนाँ เคฎंเคœ़िเคฒ เคฎें เคนै,
เคธเคฐเคซ़เคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै

เคตो เคœिเคธ्เคฎ เคญी เค•्เคฏा เคœिเคธ्เคฎ เคนै เคœिเคธเคฎे เคจ เคนो เค–़ूเคจ-เค-เคœुเคจूเคจ
เค•्เคฏा เคฒเคก़े เคคूเคซ़ाเคจ เคธे เคœो เค•เคถ्เคคी-เค-เคธाเคนिเคฒ เคฎें เคนै
เคธเคฐเคซ़เคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै
เคฆेเค–เคจा เคนै เคœ़ोเคฐ เค•िเคคเคจा เคฌाเคœ़ू-เค-เค•़ाเคคिเคฒ เคฎें

---------------------------------------------------------------------
- By Ram Prasad Bismil


เคธเคฐเคซเคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै,
เคฆेเค–เคจा เคนै เคœोเคฐ เค•िเคคเคจा เคฌाเคœुเค เค•ाเคคिเคฒ เคฎें เคนै ।
เค•เคฐเคคा เคจเคนीं เค•्เคฏों เคฆुเคธเคฐा เค•ुเค› เคฌाเคคเคšीเคค,
เคฆेเค–เคคा เคนूँ เคฎैं เคœिเคธे เคตो เคšुเคช เคคेเคฐी เคฎเคนเคซिเคฒ เคฎैं เคนै ।
เคฐเคนเคฌเคฐ เคฐाเคนे เคฎौเคนเคฌ्เคฌเคค เคฐเคน เคจ เคœाเคจा เคฐाเคน เคฎें
เคฒเคœ्เคœเคค-เค-เคธेเคนเคฐा เคจเคตเคฐ्เคฆी เคฆूเคฐिเคฏे-เคฎंเคœिเคฒ เคฎें เคนै ।
เคฏों เค–เคก़ा เคฎौเค•เคคเคฒ เคฎें เค•ाเคคिเคฒ เค•เคน เคฐเคนा เคนै เคฌाเคฐ-เคฌाเคฐ
เค•्เคฏा เคคเคฎเคจ्เคจा-เค-เคถเคนाเคฆเคค เคญी เค•िเคธी เค•े เคฆिเคฒ เคฎें เคนै ।
เค เคถเคนीเคฆे-เคฎुเคฒ्เค•ो-เคฎिเคฒ्เคฒเคค เคฎैं เคคेเคฐे เคŠเคชเคฐ เคจिเคธाเคฐ
เค…เคฌ เคคेเคฐी เคนिเคฎ्เคฎเคค เค•ा เคšเคฐ्เคšा เค—़ैเคฐ เค•ी เคฎเคนเคซिเคฒ เคฎें เคนै ।
เคตเค•्เคค เค†เคจे เคฆे เคฌเคคा เคฆेंเค—े เคคुเคे เค เค†เคธเคฎां,
เคนเคฎ เค…เคญी เคธे เค•्เคฏा เคฌเคคाเคฏें เค•्เคฏा เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै ।
เค–ींเคš เค•เคฐ เคฒाเคˆ เคนै เคธเคฌ เค•ो เค•เคค्เคฒ เคนोเคจे เค•ी เค‰เคฎ्เคฎींเคฆ,
เค†เคถिเค•ों เค•ा เคœเคฎเค˜เคŸ เค†เคœ เค•ूंเคšे-เค-เค•ाเคคिเคฒ เคฎें เคนै ।
เคธเคฐเคซเคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै,
เคฆेเค–เคจा เคนै เคœोเคฐ เค•िเคคเคจा เคฌाเคœुเค เค•ाเคคिเคฒ เคฎें เคนै ।
__
เคฐเคนเคฌเคฐ - Guide
เคฒเคœ्เคœเคค - tasteful
เคจเคตเคฐ्เคฆी - Battle
เคฎौเค•เคคเคฒ - Place Where Executions Take Place, Place of Killing
เคฎिเคฒ्เคฒเคค - Nation, faith
-------------------------------------------------------------------------------------

 'Rang De Basanti'. Here it goes -- though remember these lines are not part of the original poem written by 'Ram Prasad Bismil'.

เคนै เคฒिเคฏे เคนเคฅिเคฏाเคฐ เคฆुเคถ्เคฎเคจ เคคाเค• เคฎे เคฌैเค ा เค‰เคงเคฐ
เค”เคฐ เคนเคฎ เคคैเคฏ्เคฏाเคฐ เคนैं เคธीเคจा เคฒिเคฏे เค…เคชเคจा เค‡เคงเคฐ
เค–ूเคจ เคธे เค–ेเคฒेंเค—े เคนोเคฒी เค—เคฐ เคตเคคเคจ เคฎुเคถ्เค•िเคฒ เคฎें เคนै
เคธเคฐเคซเคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै
เคนाเคฅ เคœिเคจเคฎें เคนो เคœुเคจूเคจ เค•เคŸเคคे เคจเคนी เคคเคฒเคตाเคฐ เคธे
เคธเคฐ เคœो เค‰เค  เคœाเคคे เคนैं เคตो เคुเค•เคคे เคจเคนीं เคฒเคฒเค•ाเคฐ เคธे
เค”เคฐ เคญเคกเค•ेเค—ा เคœो เคถोเคฒा เคธा เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै
เคธเคฐเคซเคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै
เคนเคฎ เคคो เค˜เคฐ เคธे เคจिเค•เคฒे เคนी เคฅे เคฌांเคงเค•เคฐ เคธเคฐ เคชे เค•เฅžเคจ
เคœाเคจ เคนเคฅेเคฒी เคฎें เคฒिเคฏे เคฒो เคฌเคข เคšเคฒे เคนैं เคฏे เค•เคฆเคฎ
เคœिंเคฆเค—ी เคคो เค…เคชเคจी เคฎेเคนเคฎाเคจ เคฎौเคค เค•ी เคฎเคนเฅžिเคฒ เคฎैं เคนै
เคธเคฐเคซเคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै
เคฆिเคฒ เคฎे เคคूเคซाเคจों เค•ी เคŸोเคฒी เค”เคฐ เคจเคธों เคฎें เค‡เคจ्เค•เคฒाเคฌ
เคนोเคถ เคฆुเคถ्เคฎเคจ เค•े เค‰เคกा เคฆेंเค—े เคนเคฎे เคฐोเค•ो เคจ เค†เคœ
เคฆूเคฐ เคฐเคน เคชाเคฏे เคœो เคนเคฎเคธे เคฆเคฎ เค•เคนाँ เคฎंเคœिเคฒ เคฎे เคนै
เคธเคฐเคซเคฐोเคถी เค•ी เคคเคฎเคจ्เคจा เค…เคฌ เคนเคฎाเคฐे เคฆिเคฒ เคฎें เคนै

Monday, January 21, 2013

Reading a Remote File Using PHP

To read a remote file from php you have at least four options :
1. Use fopen()
2. Use file_get_contents()
3. CURL
4. Make your own function using php's socket functions.
First i need to warn you about something. You can only use fopen() and file_get_contents() when fopen wrappers is enabled. This parameter is specified in the php.ini file and cannot be changed at run time using ini_set(), To know whether you can use these two or not you can use the following code to check the value of fopen wrapper seting.
if (ini_get('allow_url_fopen') == '1') {
   // use fopen() or file_get_contents()
} else {
   // use curl or your custom function
}

1 . Using fopen()

If you use fopen() to read a remote file the process is as simple as reading from a local file. The only difference is that you will specify the URL instead of the file name. Take a look at the example below :
// make sure the remote file is successfully opened before doing anything else
if ($fp = fopen('http://www.google.com/', 'r')) {
   $content = '';
   // keep reading until there's nothing left
   while ($line = fread($fp, 1024)) {
      $content .= $line;
   }
   // do something with the content here
   // ...
} else {
   // an error occured when trying to open the specified url
}
Now, the code above use fread() function in the while loop to read up to 1024 bytes of data in a single loop. That code can also be written like this :
// make sure the remote file is successfully opened before doing anything else
if ($fp = fopen('http://www.google.com/', 'r')) {
   $content = '';
   // keep reading until there's nothing left
   while ($line = fgets($fp, 1024)) {
      $content .= $line;
   }
   // do something with the content here
   // ...
} else {
   // an error occured when trying to open the specified url
}
instead of fread() we use fgets() which reads one line of data up to 1024 bytes. The first code is much more preferable than the second though. Just imagine if the remote file's size is 50 kilobytes and consists of 300 lines. Using the first code will cause the loop to be executed about fifty times but using the second the loop will be executed three hundred times.
If you consider the cost to call a function plus the time required to make 300 requests compared to just 5 then clearly the first one is the winner.

2. Using file_get_contents()

This is my favorite way of reading a remote file because it is very simple. Just call this function and specify a url as the parameter. But make sure you remember to check the return value first to determine if it return an error before processing the result
$content = file_get_contents('http://www.google.com/');
if ($content !== false) {
   // do something with the content
} else {
   // an error happened
}

3. CURL

Unlike the two methods above using CURL cannot be said as straigthforward. Although this library is very useful to connect and communicate with may different protocols ( not just http ) it requires more effort to learn. And another problem is that not all web host have this library in their php installation. So we better make sure to check if the library is installed before trying to use it.
Here is a basic example on fetching a remote file
// make sure curl is installed
if (function_exists('curl_init')) {
   // initialize a new curl resource
   $ch = curl_init();

   // set the url to fetch
   curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');

   // don't give me the headers just the content
   curl_setopt($ch, CURLOPT_HEADER, 0);

   // return the value instead of printing the response to browser
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

   // use a user agent to mimic a browser
   curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');

   $content = curl_exec($ch);

   // remember to always close the session and free all resources
   curl_close($ch);
} else {
   // curl library is not installed so we better use something else
}
In some cases using CURL is faster than using file_get_contents() or fopen(). This is because CURL handles compression protocols by default ( for example gzip ). Many sites, big and small, use gzip compression to compress their web pages in order to save bandwidth. This site, for example, also use gzip compression which cut the bandwidth used into half. So if you're the type who just can't wait CURL will fit you most.


4. Custom functions

In the worst case your server will have fopen wrappers disabled and don't have CURL library installed. In this sad situation you just have to make your own way.
Our function shall be named getRemoteFile() which takes only one parameter, the url for the remote file. The skeleton for this function is shown below
function getRemoteFile($url)
{
   // 1. get the host name and url path

   // 2. connect to the remote server

   // 3. send the necessary headers to get the file

   // 4. retrieve the response from the remote server

   // 5. strip the headers

   // 6. return the file content
}
To extract the host name and url path from the given url we'll use parse_url() function. When given a url this function will spit out the followings :
  • scheme
  • host
  • port
  • user
  • pass
  • path
  • query
  • fragment
For example, if the url is http://www.php-mysql-tutorial.com/somepage.php then parse_url() will return :
Array
(
    [scheme] => http
    [host] => www.php-mysql-tutorial.com
    [path] => /somepage.php
)
and if the url is http://myusername:mypassword@www.php-mysql-tutorial.com/somepage.php?q=whatsthis#ouch then parse_url() will return this :
Array
(
    [scheme] => http
    [host] => www.php-mysql-tutorial.com
    [user] => myusername
    [pass] => mypassword
    [path] => /somepage.php
    [query] => q=whatsthis
    [fragment] => ouch
)
For our new function we only care about the host, port, path and query.
To establish a connection to a remote server we use fsockopen(). This function requires five arguments, the hostname, port number, a reference for error number, a reference for the error message and timeout
function getRemoteFile($url)
{
   // get the host name and url path
   $parsedUrl = parse_url($url);
   $host = $parsedUrl['host'];
   if (isset($parsedUrl['path'])) {
      $path = $parsedUrl['path'];
   } else {
      // the url is pointing to the host like http://www.mysite.com
      $path = '/';
   }

   if (isset($parsedUrl['query'])) {
      $path .= '?' . $parsedUrl['query'];
   }

   if (isset($parsedUrl['port'])) {
      $port = $parsedUrl['port'];
   } else {
      // most sites use port 80
      $port = '80';
   }

   $timeout = 10;
   $response = '';    // connect to the remote server
   $fp = @fsockopen($host, $port, $errno, $errstr, $timeout );
   if( !$fp ) {
      echo "Cannot retrieve $url";
   } else {
      // send the necessary headers to get the file
      fputs($fp, "GET $path HTTP/1.0\r\n" .
                 "Host: $host\r\n" .
                 "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3\r\n" .
                 "Accept: */*\r\n" .
                 "Accept-Language: en-us,en;q=0.5\r\n" .
                 "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" .
                 "Keep-Alive: 300\r\n" .
                 "Connection: keep-alive\r\n" .
                 "Referer: http://$host\r\n\r\n");
      // retrieve the response from the remote server
      while ( $line = fread( $fp, 4096 ) ) {
         $response .= $line;
      }
      fclose( $fp );

      // strip the headers
      $pos      = strpos($response, "\r\n\r\n");
      $response = substr($response, $pos + 4);
   }
   // return the file content
   return $response;
}
The code above sends nine lines of headers but only the first two is mandatory. So even if you send only these
fputs($fp, "GET $path HTTP/1.0\r\n" .
           "Host: $host\r\n\r\n");
the function will likely be working correctly. Not always though. Since the file is stored in a remote server It really up to that server to reply to your request or not. Some people code their page to block any request without the proper referer header. Some will only accept a specific user agent. Other will require cookies set in the header.
If you want to see what headers should be sent to successfully fetch a specific remote file try using firefox plus live http headers plugin. It's really a useful little tool

how to see xml preview in browser php

xml preview view in browser $xml = new DOMDocument('1.0', 'UTF-8');                 $xml->preserveWhiteSpace = false; ...