PHP Quick Function For Asynchronous Multi Curl

PHP Quick Function For Asynchronous Multi Curl

If you are here, it probably means that you have a problem – you have a bunch of URLs that you need to cURL, but doing it linearly (one by one) is too slow. This is where curl_multi_init comes in. It is the asynchronous version of curl and it will help you to save lots of time (by that i mean like 80-90%)

So anyway, below is a quick function for you to use. Feel free to modify it according to your needs.

function get_content_of_urls($urls)
{
    // array of curl handles
    $multi_curl = array();
    // data to be returned
    $results = array();
    // init multi handle
    $mh = curl_multi_init();
    $i = 0;
    foreach ($urls as $url) 
    {	
        $multi_curl[$i] = curl_init();
        curl_setopt($multi_curl[$i], CURLOPT_URL,$url);	
        curl_setopt($multi_curl[$i], CURLOPT_HEADER, 0); 
        curl_setopt($multi_curl[$i], CURLOPT_RETURNTRANSFER,1);
        curl_multi_add_handle($mh, $multi_curl[$i]);
        $i++;
    }
    $index=null;
    do {
        curl_multi_exec($mh,$index);
    } while($index > 0);
    // get content and remove handles
    foreach($multi_curl as $k => $ch) {
        $results[$k] = curl_multi_getcontent($ch);
        curl_multi_remove_handle($mh, $ch);
    }
    return $results;
}

If you require any assistance, feel free to let me know in the comments below.

Enjoyed the content ? Share it with your friends !

Setup PHP WordPress Dev Envrionment for Windows

Setup PHP WordPress Dev Envrionment

Requirements :

    1. Have a local php dev environment setup (MAMP/XAMPP/..)

If you have not already setup your local dev environment, you can do so by following my tutorial here

Now that you have a local php dev environment, installing WordPress is very easy.

  1. Go to the wordpress download page here and download the latest wordpress.
  2. Extract the archive to the directory of your web server. The default for XAMPP is C:\xampp\htdocs
  3. Start up Apache and MySQL
  4. Open a browser and go to http://localhost/wordpress/ and you will be greeted a page as shown below
  5. Go to your phpMyAdmin and create a database named wordpress_dev. You need not create any tables. You can refer to the images below
    xampp phpmyadmin
    xampp phpmyadmin
    xampp phpmyadmin home
    xampp phpmyadmin home
    xampp phpmyadmin create tables
    xampp phpmyadmin create tables

    xampp phpmyadmin create database
    xampp phpmyadmin create database
  6. Go back to the browser and continue with the installation, filling up the necessary details. The default username and password for MySQL in XAMPP is root and nothing/blank(there is no password)
  7. Click Continue and setup some of your other WordPress settings
  8. Your done !
Enjoyed the content ? Share it with your friends !

WordPress Code To Improve Bulk Insert Or Update Post Speed

WordPress Code To Improve Bulk Insert Or Update Post Speed

When you hit tens of thousands of posts in WordPress, the speed of wp_insert_post or wp_update_post gets significantly slower. To improve the speed, you simply add the following code and you will see that the speed improves significantly.

Add these two lines of code before your insert or update loop

wp_defer_term_counting(true); 
wp_defer_comment_counting(true);

And add these two lines after your insert or update loop

wp_defer_term_counting(false);
wp_defer_comment_counting(false);

By adding these few lines of code, you are temporarily disabling the term counts until all posts have been inserted or updated to speed up the process

Enjoyed the content ? Share it with your friends !

Virtualmin How To Change PHP Execution Time Limit

Virtualmin How To Change PHP Execution Time Limit

In order to change the execution time limit of your scripts, you will need to change 2 options in Virtualmin. First, which you should probably already know is the PHP max_execution_time variable. Secondly, you will need to change the Maximum PHP Script Run Time option.

Changing the PHP maximum_execution_time variable
  1. Assuming you have already entered Virtualmin and selected the domain you want to manage, click on the Services category.
  2. It will then open up to more options. Click either PHP 5 Configuration or PHP 7.0 Configuration according to what your domain is using (by default domains use PHP 7.0)1. change php 5 or 7 timeout limit
  3. Finally, change the timeout you want accordingly as shown below. Use the value 0 for unlimited or infinite execution time

Changing the Maximum PHP Script Run Time Option
  1. Click on the Server Configuration category
  2. Click the sub-category Website Options
  3. You should see the Maximum PHP Script Run Time option. Change it to whatever you wish. Use the value 0 for unlimited or infinite execution time2. change website options timeout limit

If you require any assistance, please feel free to ask in the comments below.

Enjoyed the content ? Share it with your friends !

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 !