Tag: php

  • How to get Contents of a Webpage using PHP curl

    How to get Contents of a Webpage using PHP curl

    PHP cURL module allows you to connect to remote websites and API endpoints to exchange data. To see if you have php-curl enabled, you can check phpinfo() page or run the command “php -m’ and look for curl.

    boby@sok-01:~$ php -m | grep curl
    curl
    boby@sok-01:~$ 
    

    If php-curl module is not installed, you can install it on Ubuntu/Debian with the command

    apt-get install php-curl
    

    To access the content of a remote web server or API endpoint, you can use the following PHP code.

    
    

    To make script work with invalid or self signed SSL, add following

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    

    If you want to make a POST request, you can use the following code

     'Yujin Boby',
        'email' => '[email protected]'
    );
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3');
    $response = curl_exec($ch);
    curl_close($ch);
    

    Back to PHP

  • How to install PHP PECL Extension

    How to install PHP PECL Extension

    PECL is a repository for PHP extensions. To install PHP extensions from PCEL, you can use the command

    pcel install EXTENSTION_NAME_HERE
    

    To install a specific version, you can specify version after extension name as follows.

    pecl install pecl_http-3.2.3
    

    Once an extension is installed, you need to edit PHP configuration and add a line to load the PHP module.

    extension=EXTENSTION_NAME_HERE.so
    

    Back to PECL

  • Install PHP 7.2 mcrypt extension on CWP Server

    Install PHP 7.2 mcrypt extension on CWP Server

    The mcrypt extension has been abandonware for nearly a decade and was deprecated in PHP 7.1, It has been removed from PHP 7.2. On PHP 7.2 or newer, you can manually install mcrypt from PCEL if you use legacy PHP code that still uses outdated mcrypt extension.

    To install PHP mcrypt module on PHP 7.2 in CWP server, do the following.

    cd /usr/local/src
    wget https://pecl.php.net/get/mcrypt-1.0.4.tgz
    tar xvf mcrypt-1.0.4.tgz
    cd mcrypt-1.0.4
    /opt/alt/php72/usr/bin/phpize
    ./configure -with-php-config=/opt/alt/php72/usr/bin/php-config
    make
    make install
    

    This will install the extension in folder /opt/alt/php72/usr/lib/php/extensions/no-debug-non-zts-20170718/. Copy it to php-fpm extension folder.

    cp /opt/alt/php72/usr/lib/php/extensions/no-debug-non-zts-20170718/mcrypt.so /opt/alt/php-fpm72/usr/lib/php/extensions/no-debug-non-zts-20170718/
    

    Create file mcrypt.ini for both php-fpm and php cli.

    echo "extension=mcrypt.so" > /opt/alt/php72/usr/php/php.d/mcrypt.ini
    echo "extension=mcrypt.so" > /opt/alt/php-fpm72/usr/php/php.d//mcrypt.ini
    

    Now you need to restart php-fpm with command

    systemctl restart php-fpm72
    

    Or though CWP admin area

    CWP Admin > PHP Settings > PHP-FPM Selector
    

    centos panel restart php-fpm service

    Back to CentOS Web Panel (CWP)

  • Install PHP APC Cache on CentOS 7 using yum

    Install PHP APC Cache on CentOS 7 using yum

    To install APC cache on CentOS 7, run

    yum install php-pecl-apcu
    

    After that, you need to restart the Apache webserver.

    systemctl restart httpd
    

    in phpinfo() page, you will see

    php APC cache in phpinfo

    Files in the packages are

    [root@localhost ~]# rpm -q --filesbypkg  php-pecl-apcu
    php-pecl-apcu             /etc/php-zts.d/40-apcu.ini
    php-pecl-apcu             /etc/php.d/40-apcu.ini
    php-pecl-apcu             /usr/lib64/php-zts/modules/apcu.so
    php-pecl-apcu             /usr/lib64/php/modules/apcu.so
    php-pecl-apcu             /usr/share/doc/pecl/apcu
    php-pecl-apcu             /usr/share/doc/pecl/apcu/NOTICE
    php-pecl-apcu             /usr/share/doc/pecl/apcu/README.md
    php-pecl-apcu             /usr/share/doc/pecl/apcu/TECHNOTES.txt
    php-pecl-apcu             /usr/share/licenses/php-pecl-apcu-5.1.21
    php-pecl-apcu             /usr/share/licenses/php-pecl-apcu-5.1.21/LICENSE
    php-pecl-apcu             /var/lib/pear/pkgxml/php-pecl-apcu.xml
    [root@localhost ~]# 
    

    Config file located at

    /etc/php.d/40-apcu.ini
    

    Some configuration settings

    apc.shm_size – default is 64M, it is better increase this value to 128M

    apc.stat – default 1, set it to 0 to increase performance. Setting it to 0 means APC won’t look for code change. When you change code, you will need to restart web server.

    apc.num_files_hint – Tell APC how many PHP files it needs to cache. Set it to 10000

  • Install PHP 5.6 on AlmaLinux 8

    Install PHP 5.6 on AlmaLinux 8

    To install PHP 5.6 on AlmaLinux 8, first enable the epel repository.

    dnf install -y epel-release

    install remi repository with the command

    dnf install http://rpms.remirepo.net/enterprise/remi-release-8.rpm

    Now you can install PHP 5.6 with

    dnf install php56

    You can execute PHP with the command

    php56

    If you want PHP 5.6 to work with the command “php”, then edit

    vi ~/.bashrc

    At end of the file, add

    source /opt/remi/php56/enable

    Log off and login to the server or run command

    source /opt/remi/php56/enable

    Or you can create a symlink

    ln -s /opt/remi/php56/root/usr/bin/php /usr/bin/php

    If you need PHP work with Apache, run install Apache with

    dnf install httpd

    Install php-fpm with

    dnf install php56-php-fpm

    Enable and restart Apache

    systemctl enable php56-php-fpm
    systemctl enable httpd
    systemctl restart httpd
    systemctl restart php56-php-fpm

    php-fpm pool config file available at

    /etc/opt/remi/php56/php-fpm.d/www.conf
  • Warning: Use of undefined constant OAUTH_REQENGINE_CURL

    Warning: Use of undefined constant OAUTH_REQENGINE_CURL

    On an Ubuntu server (Ubuntu 20.04.2 LTS) when running a PHP script, I get the following error.

    Warning: Use of undefined constant OAUTH_REQENGINE_CURL - assumed 'OAUTH_REQENGINE_CURL' (this will throw an Error in a future version of PHP
    

    On checking phpinfo, I get

    Ubuntu PHP OAuth

    Request engine support	php_streams
    

    curl is missing in “Request engine support”.

    This is because the default php-oauth package does not support curl. To fix, uninstall php-oauth and install oauth package using pecl.

    Uninstall pcel

    apt install php-oauth -y
    

    install curl dev package

    apt install libcurl4-gnutls-dev
    ln -s /usr/include/x86_64-linux-gnu/curl /usr/include/curl
    

    Install PHP PECL

    apt install php-pear
    

    Install oAuth

    pecl install oauth
    

    Edit php.ini file

    vi /etc/php/7.4/cli/php.ini
    

    Add at end of the file

    extension=oauth.so
    

    You need to do the same for Apache and php-fpm php.ini files located at

    /etc/php/7.4/apache2/php.ini
    /etc/php/7.4/fpm/php.ini
    

    7.4 is for PHP version 7.4. Change this to whatever PHP version you have on your server. After making the change, phpinfo() page shows curl

    ubuntu php oauth

    See php

  • Install PHP drivers for Microsoft SQL Server on Ubuntu PHP 7.2

    Install PHP drivers for Microsoft SQL Server on Ubuntu PHP 7.2

    On Ubuntu 18.04 server running PHP 7.2, i want to install Microsoft SQL Server module for PHP. You can find PHP module for SQL server at

    https://github.com/microsoft/msphpsql

    At the time of writing this PHP module only support PHP 7.4 and newer. SO i need to find older version that supported PHP 7.2. On checking release page, i found version 5.8.0 supported PHP 7.2

    First install php7.2 dev package with

    apt install php7.2-dev
    

    Instal php modules with pcel

    pecl install sqlsrv-5.8.0
    

    During install, i got error

    configure: creating ./config.status
    config.status: creating config.h
    config.status: executing libtool commands
    running: make
    /bin/bash /tmp/pear/temp/pear-build-rootnuxjAy/sqlsrv-5.8.0/libtool --mode=compile g++ -std=c++11 -I. -I/tmp/pear/temp/sqlsrv -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-rootnuxjAy/sqlsrv-5.8.0/include -I/tmp/pear/temp/pear-build-rootnuxjAy/sqlsrv-5.8.0/main -I/tmp/pear/temp/sqlsrv -I/usr/include/php/20170718 -I/usr/include/php/20170718/main -I/usr/include/php/20170718/TSRM -I/usr/include/php/20170718/Zend -I/usr/include/php/20170718/ext -I/usr/include/php/20170718/ext/date/lib -I/tmp/pear/temp/sqlsrv/shared/  -DHAVE_CONFIG_H  -std=c++11 -D_FORTIFY_SOURCE=2 -O2 -fstack-protector   -c /tmp/pear/temp/sqlsrv/conn.cpp -o conn.lo
    libtool: compile:  g++ -std=c++11 -I. -I/tmp/pear/temp/sqlsrv -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-rootnuxjAy/sqlsrv-5.8.0/include -I/tmp/pear/temp/pear-build-rootnuxjAy/sqlsrv-5.8.0/main -I/tmp/pear/temp/sqlsrv -I/usr/include/php/20170718 -I/usr/include/php/20170718/main -I/usr/include/php/20170718/TSRM -I/usr/include/php/20170718/Zend -I/usr/include/php/20170718/ext -I/usr/include/php/20170718/ext/date/lib -I/tmp/pear/temp/sqlsrv/shared/ -DHAVE_CONFIG_H -std=c++11 -D_FORTIFY_SOURCE=2 -O2 -fstack-protector -c /tmp/pear/temp/sqlsrv/conn.cpp  -fPIC -DPIC -o .libs/conn.o
    In file included from /tmp/pear/temp/sqlsrv/shared/typedefs_for_linux.h:23:0,
                     from /tmp/pear/temp/sqlsrv/shared/xplat_winnls.h:24,
                     from /tmp/pear/temp/sqlsrv/shared/FormattedPrint.h:24,
                     from /tmp/pear/temp/sqlsrv/shared/core_sqlsrv.h:41,
                     from /tmp/pear/temp/sqlsrv/php_sqlsrv_int.h:25,
                     from /tmp/pear/temp/sqlsrv/conn.cpp:24:
    /tmp/pear/temp/sqlsrv/shared/xplat.h:30:10: fatal error: sql.h: No such file or directory
     #include 
              ^~~~~~~
    compilation terminated.
    Makefile:194: recipe for target 'conn.lo' failed
    make: *** [conn.lo] Error 1
    ERROR: `make' failed
    root@server:~# 
    

    This is fixed with command

    apt-get install unixodbc-dev
    

    install pdo_sqlsrv with

    pecl install pdo_sqlsrv-5.8.0
    

    Run

    printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/7.2/mods-available/sqlsrv.ini
    printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/7.2/mods-available/pdo_sqlsrv.ini
    

    Enabe PHP modules with

    phpenmod -v 7.2 sqlsrv pdo_sqlsrv
    

    Restart Apache, now phpinfo() shows pdo_sqlsrv

    But when accessing PHP script that connect to MS SQL server, i get error

    This extension requires the Microsoft ODBC Driver for SQL Server to communicate with SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x64

    To fix this, do

    curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
    

    For Ubuntu 16.04

    curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
    

    For Ubuntu 18.04

    curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
    

    For Ubuntu 20.04

    curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
    

    Ubuntu 20.10

    curl https://packages.microsoft.com/config/ubuntu/20.10/prod.list > /etc/apt/sources.list.d/mssql-release.list
    

    Update apt cahe

    apt-get update
    

    Install Microsoft ODBC

    apt-get install -y msodbcsql17
    

    Optional: for bcp and sqlcmd

    apt-get install -y mssql-tools
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc
    apt-get install -y unixodbc-dev
    

    Now php MS SQL module will work. You can find sample PHP code at

    https://gist.github.com/serverok/456b3d1d7295463df42c9822e8db3e5b
    https://github.com/microsoft/msphpsql/blob/master/sample/pdo_sqlsrv_sample.php

    Here are microsoft documentation

    https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017

    https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15

    See PHP

  • Redirect a site to www using PHP

    To redirect a site to URL with www using PHP, you can use the following PHP code

    <?php
    
    $domainName = $_SERVER['HTTP_HOST'];
    
    if (strpos($domainName, 'www') === false) {
        header("HTTP/1.1 301 Moved Permanently");
        $urlNew = 'https://www.' . $domainName .  $_SERVER['REQUEST_URI'];
        Header("Location: $newUrl");
        exit;
    }

    Add this code in your index.php file or another file included by index.php

    See Redirect

  • Install PHP 5.6 on CentOS 7

    To install PHP 5.6 on CentOS 7, enable EPEL repo and remi repo

    yum install epel-release -y
    rpm -ivh https://rpms.remirepo.net/enterprise/remi-release-7.rpm
    

    Install yum-utils and enable remi-php56 repo

    yum install yum-utils
    yum-config-manager --enable remi-php56
    

    Install PHP 5.6 with

    yum install -y php php-bcmath php-cli php-common php-devel php-gd \
        php-imap php-intl php-json php-ldap php-lz4 php-mbstring php-mysqlnd \
        php-soap php-intl php-opcache php-xml php-pdo
    
  • Install LAMP Server on Ubuntu/Debian

    To install LAMP (Apache, MySQL, PHP) on Ubuntu/Debian web server, run

    apt-get install lamp-server^
    

    This use meta package install LAMP. If you want to remove, don’t remove the meta package as it will remove many other required packages. You need to remove packages one by one.

    Related Posts

    Apache

    MySQL

  • HTTP 301 Redirect using PHP

    HTTP 301 Redirect using PHP

    You can use the PHP header() function to do redirect.

    To do 301 redirect, use

    header('Location: https://new-url/', true, 301);
    exit;

    For 302 redirect, use

    header('Location: https://new-url/');
    exit;

    If you need to redirect a site to a new site while keeping the same URL structure, you can use

    $newUrl = "https://NEW-URL-HERE" . $_SERVER['REQUEST_URI'];
    header("HTTP/1.1 301 Moved Permanently");
    Header("Location: $newUrl");
    exit;

    Back to redirect