Freelance Web Development Resource

web development resources – dolrich fortich

RSS
people

Simple curl sample codes

Here are some simple curl codes I commonly use.

#1 – Fetch url content (basic GET request)

$curl_url = 'http://www.example.com/sample.php';

$ch=curl_init($curl_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_result = curl_exec($ch);
curl_close($ch);

#2 – Fetch url content (basic POST request)

$curl_post_data = array(
	'variable_1' => 'data_1',
	'variable_2' => 'data_2'
	);
$curl_url = 'http://www.example.com/sample.php';

$ch = curl_init ($curl_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($curl_post_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_result = curl_exec ($ch);
curl_close ($ch);

echo $curl_result;

#3 – Curl request with session


#Tips in case the code above is not working

1. The site probably restricts request not sent by common browsers. To imitate a browser, add the curl option below.

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
				'Host: example.com',
				'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0',
				'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
				'Accept-Language: en-us,en;q=0.5',
				'Connection: keep-alive',
				));

2. Youre probably accessing an https site.

<code>
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);</code>

More info regarding this,

http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

3. Check if there is an error

Place this before closing the curl connection.

$curl_error = curl_error($ch);
No Comments |

Count numbers in a string

Function to count numbers in a string.
Ex: (123) 456-7891
Numbers in a string: 10

function count_numbers($string)
{
  preg_match_all('/[0-9]/', $string, $matches);
  return count($matches[0]);
}

$string = '(123) 456-7891';
echo count_numbers($string);
No Comments |

Determining the delimeter used in a CSV file

Just today, I was tasked to create an application for a client to process
google adwords reports from a csv file.

Everything went well till more csv files were uploaded, some of the csv files were not really
comma(,) separated. The function below did solve the problem.

function get_csv_delimeter($csv_text)
{
	$delimeters = array(',',';','|','~',"\t");
	$current_delimeter_count = 0;
	$current_delimeter = FALSE;

	foreach($delimeters as $delimeter)
	{
		if(substr_count($csv_text, $delimeter) > $current_delimeter_count)
		{
			$current_delimeter = $delimeter;
		}
	}

	return $current_delimeter;
}

You can see here part of the code that process the csv file. What it does is
to place the data into an array.

<?php
$csv_data = array();

if(isset($_FILES['csv']) AND
	$_FILES['csv']['type'] == 'text/csv' AND
	! $_FILES['csv']['error'])
{
	$row = 1;
	if (($handle = fopen($_FILES['csv']['tmp_name'], "r")) !== FALSE) {

		//Reading the first line from the file
		if(($data = fgets($handle)) !== FALSE) {

			//The function used to detect determine the delimeter
			$delimeter = get_csv_delimeter($data);

			//Reset the file pointer to the beginning
			rewind($handle);
		}

		while (($data = fgetcsv($handle, 1000, $delimeter)) !== FALSE) {

			for ($c=0; $c < $num; $c++) {
				$csv_data[$row][$c] = utf8_encode(preg_replace('/[^(\x20-\x7F)\x0A]*/','', $data[$c]));
			}
			$row++;
		}

		fclose($handle);
	}
}
No Comments |

Grouping of WHERE statement using CodeIniter Active Record

To generate this SQL query using CodeIgniter Active Record.

SELECT `id` FROM (`users`)
		WHERE
			(first_name = 'test1' AND age =18) OR
			(last_name = 'test2' AND age =17)

You can do it like this.

$first_name = 'test1';
$last_name = 'test2';
$age1 = 18;
$age2 = 17;

$this->db->select('id')
  ->from('users')

  ->where('(first_name', $first_name)
  ->where('age', $this->db->escape($age1).')', FALSE)

  ->or_where('(last_name', $last_name)
  ->where('age', $this->db->escape($age2).')', FALSE)

  ->get();

Let me know if you have a better way to do this.

No Comments |

Calculating age in javascript

Here is method in calculating age in javascript. Its pretty simple, based from a sample age calculation code written in php which you can find here http://www.bradino.com/php/calculate-age/

function calculate_age(yr, mon, day)
{
 var d=new Date(Date.UTC(yr,mon-1,day,0,0,0)).getTime();
 var d1=new Date().getTime();
 return Math.floor((Math.round(d1 / 1000) - Math.round(d / 1000))/31556926);
}

Sample usage.

document.write(calculate_age(1956, 5, 24));
No Comments |