Skip to content

Extended Functions for Data Source Development V3

tm1000 edited this page Jul 20, 2012 · 4 revisions

There are 4 principle functions available to the caller ID sources.

They are:

  • SearchURL Returns the first matched element in a URL. Combines the functionality of preg_match and get_url_contents into one file. Will return FALSE if no match was found. Otherwise will return result AND will pass-by-reference on $match

  • ExtractMatch Extract the CID from a preg_match'd array (also works with returned data from function SearchURL)

  • get_url_contents Returns a string containing the contents of url specified by url and other optional parameters.

  • sys_get_temp_dir Returns the system defined temporary location for files

SearchURL

Description:

_SearchURL

Parameters:

Examples:

get_url_contents

Description:

_get_url_contents (string url, [array post_data], [string referrer], [string cookie_file], [string useragent]) Returns a string containing the contents of url specified by url and other optional parameters.

Parameters:

url -- (required)

A string containing the full url of the page you would like to receive.

post_data -- (optional)

An array containing the data you would like to post. If not provided or set to false, no data will be posted.

referrer -- (optional)

A string containing the full url of the referring page. If not provided or set to false, no referrer will be used.

cookie_file -- (optional)

A string containing the full path to the file you will use to store cookie data. If not provided or set to false, cookies will not be used.

useragent -- (optional)

A string containing the user agent to masquerade as. If not provided or set to false, the default user agent is: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1

Examples:

Simple url

// Set the url
$url = "http://www.google.com";

// Get the url
$result = $this->get_url_contents($url);

Post data

// Create the post array.  The key is the form element's name, the value is the form element's value
$post_data = array(
    "form_element_name_1" => "form_element_value_1",
    "form_element_name_2" => "form_element_value_2",
    "form_element_name_3" => "form_element_value_3"
);

// Set the url of the page the form data should be submitted to
$url = "http://www.maypage.com/form_proccessing_page";

// Post the form data and return the results
$result = $this->get_url_contents($url, $post_data);

Using cookies and referrer to post data to a page requiring cookies and a referrer.

// Set the referring page.  In this case, it is the url of the page containing the form.
$referrer ="http://www.telekom.rs/WhitePages/SearchPage.asp";

// Set the url of the page that will be processing the posted data
$url="http://www.telekom.rs/WhitePages/ResultPage.asp";

// Set the data that will be posted
$post_data = array(
     'Telefon'=>$phone_number,
     'Ulica'=>'',
     'MG'=>$area_code,
     'Ime'=>'',
     'Broj'=>'',
     'Mesto'=>'',
     'Prezime'=>'',
     'submit.x'=>'58',
     'submit.y'=>'10'
);

// Create a temporary path for a cookie file in the /tmp directory
$cookie_file = tempnam($this->sys_get_temp_dir(), "CURLCOOKIE");

// Load the results page once to get some cookies set.
// The first result will fail, but the cookie file will be populated
$result = $this->get_url_contents($url,$post_data,$referrer,$cookie_file);

// Load the results page again, now that our cookie file has valid cookies
$result = $this->get_url_contents($url,$post_data,$referrer,$cookie_file);

// Delete the temporary cookie file, since we no longer need it
@unlink($cookie_file);