Category Archives: Uncategorized

PHP cURL curl_getinfo() Output

Example output of cURL curl_getinfo(): Array ( [url] => http://www.example.com/path.php [content_type] => text/html; charset=iso-8859-1 [http_code] => 302 [header_size] => 304 [request_size] => 322 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.290086 [namelookup_time] => 0.06063 [connect_time] => 0.155486 [pretransfer_time] => 0.155554 [size_upload] => 0 [size_download] => 210 [speed_download] => 723 [speed_upload] => […]

PHP cURL Get Redirect URL as Variable

Get the redirect URL with cURL and PHP: function get_redirect_url($url, $referer) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); curl_setopt($ch, CURLOPT_REFERER, $referer); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HEADER, false); $data = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); […]

PHP Find the Xth occurrence of a substring in a string

Function that use strpos() to get the Xth occurrence of a substring in a string: /** * Find the position of the Xth occurrence of a substring in a string * @param $haystack * @param $needle * @param $number integer > 0 * @return int */ function strposX($haystack, $needle, $number){ if($number == ‘1’){ return strpos($haystack, […]

PHP StrPos() Find Second Occurrence of a String

Simple PHP code to find the second occurrence of a string with strpos(): $pos1 = strpos($haystack, $needle); $pos2 = strpos($haystack, $needle, $pos1 + strlen($needle));$pos1 = strpos($haystack, $needle); $pos2 = strpos($haystack, $needle, $pos1 + strlen($needle)); You can make it a one-line like this: $pos2 = strpos($haystack, $needle, strpos($haystack, $needle) + strlen($needle));$pos2 = strpos($haystack, $needle, strpos($haystack, $needle) […]

Download a remote file with cURL and PHP

Save a remote file to disk with PHP and cURL: // Remote file to download $url = "http://www.sample-videos.com/audio/mp3/crowd-cheering.mp3";   // This is where the file will be saved $fp = fopen(dirname(__FILE__).’/localfile.tmp’, ‘w+’); // Replace spaces with %20 on the URL string $ch = curl_init(str_replace(" ","%20", $url)); // We allow cURL to run for max 60 […]

Force Download of Remote File with PHP

Force the download of a remote file with PHP: $file_name = ‘crowd-cheering.mp3’; $file_url = ‘http://www.sample-videos.com/audio/mp3/’ . $file_name; header(‘Content-Type: application/octet-stream’); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=\"".$file_name."\""); readfile($file_url); exit;$file_name = ‘crowd-cheering.mp3’; $file_url = ‘http://www.sample-videos.com/audio/mp3/’ . $file_name; header(‘Content-Type: application/octet-stream’); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=\"".$file_name."\""); readfile($file_url); exit; Note that ini.allow-url-fopen must be enabled. Alternatively, download the file to disk and […]

Upload a File via POST with cURL and PHP 7

Example of sending or uploading a file via POST with cURL and PHP 7: File content of php-send-file-curl.php: <?php if ( isset($_POST[‘submitfile’]) ) { // Make sure there are no upload errors if ($_FILES[‘upfile’][‘error’] > 0) { die("Error uploading file…"); }   // Prepare the cURL file to upload, including file name and MIME type […]

Youtube embed iframe not working on mobile devices

Some days ago I noticed that Youtube iframe embed code was not working on mobile devices, instead it showed a blank space on mobile. I used the iframe code gathered from the Youtube -> Share -> Embed window. After some testings, I found the correct Youtube embed iframe code that works on iPhone, iPad and […]

Array as parameters in a PHP function

Sample PHP function that uses an array to handle arguments: function testFunction($args) { // $args contains the arguments of the function print_r($args); // Validate and assign params to variables $name = $args[‘name’] ? $args[‘name’] : "None"; $file = $args[‘file’] ? $args[‘file’] : "None"; $title = $args[‘title’] ? $args[‘title’] : "None"; }function testFunction($args) { // $args […]

Change Bootstrap Table Border Color

Change default border color of table-bordered class: .table-bordered { border: 1px solid #ecf0f1 !important; } .table-bordered > thead > tr > th, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > tbody > tr > td, .table-bordered > tfoot > tr […]