PHP PhantomJS Change User Agent String

PHP PhantomJS Change User Agent String

Noticed that google isn’t really useful in this case (there’s no copy and paste answer). Google for me only shows a bunch of people having issues and no easy solution when in fact the solution is quite simple

First things first, you have to check to see if your PHP-PhantomJs library version is 3.x or 4.x

If it is 3.x, to change the user-agent string, you just have to do this

$request->addSetting('userAgent', 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0');

If it is 4.x, to change the user-agent string, you just have to do this

$request->addHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0');

Of course, feel free to change the new value of your user-agent to whatever you want it to be.

To verify that it is working, if your testing locally, then you can use tools such as Fiddler to check the headers. Alternatively, you can dump out the $request and $response variables, like so,

var_dump($request); // $request is the return value of either createRequest or createCaptureRequest function
var_dump($response); // $response is the return value of createResponse function

If you have any questions feel free to let me know in the comments below.

 

 

Enjoyed the content ? Share it with your friends !

PHP WordPress Remove Paragraph and Line Break Tags

PHP WordPress Remove Paragraph and Line Break Tags

I find myself needing to remove the paragraph and line break tags that are automatically added into the post or category description quite often. So here is how it can be done

For Categories or Custom Taxonomies

add_filter( 'term_description', 'do_shortcode' ); /*Enable shortcode for term_description (incl. category/other taxonomy)*/
add_filter( 'term_description', 'shortcode_unautop' ); /*Don’t auto-p wrap shortcodes that stand alone*/
add_filter( 'term_description', 'wpautop' , 100); /*Remove line breaks from term_description by pushing priority to the end. Alternatively, you can remove the filter entirely*/
remove_filter('term_description', 'wpautop'); /*Remove line break filter completely*/

For Posts

add_filter( 'the_content', 'do_shortcode' ); /*Enable shortcode for term_description (incl. category/other taxonomy)*/
add_filter( 'the_content', 'shortcode_unautop' ); /*Don’t auto-p wrap shortcodes that stand alone*/
add_filter( 'the_content', 'wpautop' , 100); /*Remove line breaks from term_description by pushing priority to the end. Alternatively, you can remove the filter entirely*/
remove_filter('the_content', 'wpautop'); /*Remove line break filter completely*/

 

Enjoyed the content ? Share it with your friends !

PHP Check If A Resource Pointed By Url Is Valid

PHP Check If A Resource Pointed By Url Is Valid

Sometimes we might have to check if a particular video or image exists and works, and to do so, some people might choose to download the entire resource file using something like file_get_contents which might not be the most effective and efficient way of doing so.

A better way is to use curl and simply check the http status code returned.

/*
* Check if a resource is valid (or exists)
* Works with pretty much anything - jpg, mp4 ...
*
* @url : the url must be a direct link (or hotlink) to the resource.
*/
function is_resource_valid($resource_url)
{
    $resource_exists = false;
     
    $ch = curl_init($resource_url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
     
    if($status_code == '200'){
        $resource_exists = true;
    }		
    
    return $resource_exists;
}

 

Enjoyed the content ? Share it with your friends !

PHP SQLite3 Check For Constraint Violation

PHP SQLite3 Check For Constraint Violation

Sometimes we may need to use a constraint in our database design (e.g UNIQUE constraint) and want to be able to handle when such violations occur for logging purposes or others.

The PHP library for SQLite3 provides a way for us to know the last error that happened – its error code and message using SQLite3::lastErrorCode and SQLite3::lastErrorMsg

From SQLite documentation on the error code list here, you can see that SQLITE_CONSTRAINT error code is 19. So to check for a constraint violation, it will look something like this

// $this->db is the SQLite3 object / class instance
$last_error_code = $this->db->lastErrorCode();
$last_error_msg  = $this->db->lastErrorMsg();
if( $this->db->lastErrorCode() == self::SQLITE3_CONSTRAINT ) {	
    // Handle CONSTRAINT violations here
}

However, the SQLite3 library for PHP does not provide a way for us to get extended error code (or at least, i’m unable to find it from their documentation page here). Hence, to check for exactly which constraint was violated (e.g UNIQUE), you will have to check the SQLite3::lastErrorMsg instead.

$last_error_msg  = $this->db->lastErrorMsg();
if( strstr($last_error_msg, 'UNIQUE') ) {
   //UNIQUE CONSTRAINT VIOLATION ...
}

 

Enjoyed the content ? Share it with your friends !

Setup PHP Dev Envrionment on Windows

Setup PHP Dev Envrionment on Windows

It is actually very simple to setup a simple development environment for your PHP projects. XAMPP is your friend you must have for this. It is easy to install and setup, with minimal configuration needed after.

Here are the steps,

  1. Go to XAMPP website to download and install it.
  2. After installation, run it and you will see a window as shown below

    XAMPP Control Panel
    XAMPP Control Panel
  3. Start APACHE and MYSQL modules and your already almost there !
  4. Now, you can open your browser and hit in http://localhost:80 for the url and you will see a welcome message by XAMPP.

Congrats ! You have setup your development environment for PHP. Here are some things to take note of
– To enable or disable modules (e.g SQLite3), you can modify the php.ini which should by default is inC:\xampp\php
– You can look through your logs and edit your config through the same control panel as well.
– If you wish to put your own html, php, javascript, .. files, you will have to go to C:\xampp\htdocs and place your files there

Enjoyed the content ? Share it with your friends !