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 |