T2A API for Search

T2A API is an API for finding UK people, addresses, phone numbers and more. http://t2a.co/
I want to use this API to "Search for a Person" , details available at http://t2a.co/docs/methods/person_search
Documentation available at http://t2a.co/docs
The search is not free.
but test environment is free which searches from dummy data. http://t2a.co/docs/index/free_test_mode
where key='test'

API parameters are given at http://t2a.co/docs/methods/person_search
but I want help how the form will be created with action = get and where it to be submitted?

If possible please guide me along with coding.

Thanks in advance.
LVL 1
Rajesh JoshiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
The form action does not need to submit to the API. It just needs to submit to the form itself. Here is an exampe PHP script (the form submits to t2api.php, so I suggest you also call the script the same: t2api.php - or just edit the form\action):
<?php
// Get text from query string or set to empty
$txtName = isset($_POST['txtName']) ? $_POST['txtName'] : '';
$txtStreet = isset($_POST['txtStreet']) ? $_POST['txtStreet'] : '';

if($txtName != '' || $txtStreet != '') {

	$url = 'http://t2a.co/rest/?'
	. 'method=person_search'
	. '&api_key=test'
	. '&name=' . $txtName
	. '&street=' . $txtStreet;

	// Fetch XML from T2A API
	$result = simplexml_load_file($url);

	if($result) {

	  // Check for errors
	  if($result->status == 'ok') {

		$person_list = $result->person_list;
		$place_list = property_exists($result, "place_list");
		$street_list = property_exists($result, "street_list");

	  } else {

		// Report error
		echo 'Error: ' . $result->error_code;
		exit;

	  }
	}

}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Code example</title>
  </head>
  <body>

	<!-- we create our form. the form will post back to itself -->
  	<form action="t2api.php" method="post">
		<input type="text" value="fawcett" name="txtName"/><br/><br/>
		<input type="text" value="" name="txtStreet"/><br/><br/>
		<input type="submit" value="run API" name="cmdSubmit"/><br/><br/>
  	</form>

    <!-- iterate over list of people -->
    <?php
    if($url) {
		echo "Calling API: <a target='_blank' href='$url'>" . $url . "</a><br/>";

		if($place_list) echo "A place list was returned: this means, the place name passed to the API is ambiguous, and the final user should be given a selection from the list.<br/>";
		if($street_list) echo "A street list was returned: this means, the street specified is ambiguous, and the final user should be given a selection from the list.<br/>";

		foreach($person_list->person as $person): ?>

			<!-- output person details -->
			<h2><?php echo $person->name_single_line; ?></h2>
			<?php echo $person->addr_single_line; ?><br/>

		<?php
		endforeach;

		//display the XML returned from the API call
		$dom = dom_import_simplexml($result)->ownerDocument;
		$dom->formatOutput = true;
		echo "<textarea rows='40' cols='150'>" . $dom->saveXML() . "</textarea>";

	}
    ?>

  </body>
</html>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
The docs and code examples appear to be pretty good.
http://iconoun.com/demo/temp_rajesh.php
<?php // demo/temp_rajesh.php

/**
 * http://www.experts-exchange.com/questions/28711967/T2A-API-for-Search.html?cid=1752
 *
 * http://t2a.co/docs/index/code_examples
 * http://t2a.co/docs/index/free_test_mode
 * http://t2a.co/docs/methods/person_search
 */
error_reporting(E_ALL);


// CONSTRUCT A QUERY
$query_array =
[ 'api_key' => 'test'
, 'output'  => 'json'
, 'method'  => 'person_search'
, 'name'    => 'Mr John D Fawcett'
]
;
$query_string = http_build_query($query_array);


// SET THE API ENDPOINT AND GET A RESPONSE
$api_endpoint = 'http://t2a.co/rest/?' . $query_string;
$response     = file_get_contents($api_endpoint);


// DECODE THE RESPONSE
$json = json_decode($response);


// PRINT SOME INFORMATION FROM THE RESPONSE
echo '<pre>';
foreach ($json->person_list as $person)
{
    echo PHP_EOL . $person->name_single_line;
    echo PHP_EOL . $person->addr_single_line;
    echo PHP_EOL;
}


// ACTIVATE THIS TO SEE THE ENTIRE RESPONSE
// var_dump($json);

Open in new window

HTH, ~Ray
Rajesh JoshiAuthor Commented:
Hi Ray Paseur,

It shows some syntax error near $query_array in Dreamweaver,
Please help.

Thanks a lot,
Rajesh.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Ray PaseurCommented:
Dreamweaver is outdated.  The "short array syntax" uses the square brackets to be more in consonance with JavaScript and JSON notation.  It has been part of PHP since PHP 5.4, more than three years ago.  You may want to consider discarding Dreamweaver in favor of another IDE.  If your PHP installation is less than current, you may want to consider upgrading to the current levels of PHP.  They are listed on the upper left side of http://php.net near the word, "Download."  You want to be at least PHP 5.4.45 today.

You may try this in place of lines 13-20 from the code snippet I posted.
// CONSTRUCT A QUERY
$query_array = array
( 'api_key' => 'test'
, 'output'  => 'json'
, 'method'  => 'person_search'
, 'name'    => 'Mr John D Fawcett'
)
;

Open in new window

Rajesh JoshiAuthor Commented:
Still it shows error.
However the code is working.
Rajesh JoshiAuthor Commented:
Thank you  MlandaT & Ray Paseur.
Ray PaseurCommented:
Still it shows error.
However the code is working.
Yes, that is the sort of thing that we have come to expect from Dreamweaver.  Dreamweaver is really a terrible way to create PHP applications.  If you want a 21st century tool, you might want to consider PHPStorm.
Rajesh JoshiAuthor Commented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.