Jump to content

How to connect to API?


cpcdev84

Recommended Posts

Hi,I'm wondering how to connect to an API? I am fairly new to XML... do I connect to it using XML or if I'm using PHP around it do I call it through PHP? Any help is appreciated. I'm trying to parse data from the API. Also, what information do I need to know about the API in order to connect to it?Thanks!

Link to comment
Share on other sites

If the API is exposed through PHP, then that is the only way to do it. I can't answer the rest unless I know what the API is. PHP has many excellent ways of parsing XML.
Thanks for the info.. it's a private API through the company I work for. There isn't any documentation on how to connect to it, just examples of how to parse the data. Could you give me an example of how to call an API through PHP?
Link to comment
Share on other sites

I think I'm heading in the right direction possibly.. I'm using this code sample

<?php// Request Yahoo! REST Web Service using// file_get_contents. PHP4/PHP5// Author: Rasmus Lerdorf and Jason Levitt// February 1, 2006error_reporting(E_ALL);$request =  'http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query='.urlencode('Al Gore').'&results=1';// Make the request$xml = file_get_contents($request);// Retrieve HTTP status codelist($version,$status_code,$msg) = explode(' ',$http_response_header[0], 3);// Check the HTTP Status codeswitch($status_code) {	case 200:		// Success		break;	case 503:		die('Your call to Yahoo Web Services failed and returned an HTTP status of 503. That means: Service unavailable. An internal problem prevented us from returning data to you.');		break;	case 403:		die('Your call to Yahoo Web Services failed and returned an HTTP status of 403. That means: Forbidden. You do not have permission to access this resource, or are over your rate limit.');		break;	case 400:		// You may want to fall through here and read the specific XML error		die('Your call to Yahoo Web Services failed and returned an HTTP status of 400. That means:  Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');		break;	default:		die('Your call to Yahoo Web Services returned an unexpected HTTP status of:' . $status_code);}// Output the XMLecho htmlspecialchars($xml, ENT_QUOTES);?>

How can I output the code so that I can style it using HTML?

Link to comment
Share on other sites

Which of your several questions do you want answered? At this point, $xml contains string data formatted as XML.1. Do you want to use your company's private API to parse it? If so, no one here can help you unless you provide documentation for the API.2. Do you want to parse it using built-in PHP functions?3. You mentioned outputting HTML. Does that mean you want to transform it using XSLT, or do you want to transform it to HTML using built-in PHP functions?

Link to comment
Share on other sites

Which of your several questions do you want answered? At this point, $xml contains string data formatted as XML.1. Do you want to use your company's private API to parse it? If so, no one here can help you unless you provide documentation for the API.2. Do you want to parse it using built-in PHP functions?3. You mentioned outputting HTML. Does that mean you want to transform it using XSLT, or do you want to transform it to HTML using built-in PHP functions?
Sorry for being vague.. I am doing my best to understand what I'm trying to solve more as I go along. Here's a link to the API documentation:Now I believe they have created some of their own functions to parse, not completely sure though. I am guessing I will want to transform it using XSLTThanks for your continued help.
Link to comment
Share on other sites

That API tells you how to get XML data based on a request. Parsing and presenting the XML data will be up to you, I think.
Thanks for the clarification. So am I starting the "parse" correctly with the code I included above? This is what I have so far: http://65.213.231.100/~wizard/lauderdalema....com/tester.php but I'm not sure if I'm pulling the data correctly to begin with.. and then how to separate it/style it from that point.I REALLY appreciate your help btw. I'd be nowhere without forums and books.
Link to comment
Share on other sites

I really wouldn't call that parsing. Except for transforming the entities, it's pretty much a data dump. You might want to look at simpleXML as a way of extracting the data and embedding it in some sort of HTML structure. (I've linked you to some examples. There's more documentation.) simpleXML isn't the best, but it's the easiest to learn.

Link to comment
Share on other sites

  • 2 weeks later...
I really wouldn't call that parsing. Except for transforming the entities, it's pretty much a data dump. You might want to look at simpleXML as a way of extracting the data and embedding it in some sort of HTML structure. (I've linked you to some examples. There's more documentation.) simpleXML isn't the best, but it's the easiest to learn.
Hello again,I am still attempting to parse the data. I've begun looking into simplexml. here's what i have so far:
<?php$btapi = 'http://api.traderonline.com/vehicles/getTypes?xml=%3CTOLData%3E%3Crealm%3Ecycle%3C%2Frealm%3E%3C%2FTOLData%3E';$btapi = simplexml_load_file($btapi); ?>

Is this the correct way to connect to an api?How can I display data from that point? I can provide the link to that API documentation again.Could you possible give me an example of how to display some data from that API?Thank you for the help. I hope i'm on the right track.

Link to comment
Share on other sites

Now i added this code:

foreach($btapi->children() as $child) {  echo $child->getName().":  "; }

and its showing the first set of children.. Is there a way to display one individual child in that set rather than all of them?Thanks!

Link to comment
Share on other sites

If you know something about the structure of the document, the examples here should help. It's the -> syntax that makes simpleXML so simple.
Thank you. Following those examples I'm now able to drill down into the sub categories using this:
echo $btapi->{'PWC'}->children()->getName();

The intention is to go into the first child called "PWC" and then into its children. I'm able to display the names of the children but how can I display the actual values and not just the names?I tried to echo the $value variable using this:

$value = (string) $btapi->{'PWC'}->children();

but it's not showing any of the values. Any suggestions?Thank you for your help. Slowly its coming together

Link to comment
Share on other sites

$element.children() returns an object, not an individual value. Have you tried something like this:

$btapi = 'http://api.traderonline.com/vehicles/getTypes?xml=%3CTOLData%3E%3Crealm%3Ecycle%3C%2Frealm%3E%3C%2FTOLData%3E';$xml = simplexml_load_file($btapi);echo $xml->PWC->ITEM[0]->CATEGORY_NAME;

Link to comment
Share on other sites

$element.children() returns an object, not an individual value. Have you tried something like this:
$btapi = 'http://api.traderonline.com/vehicles/getTypes?xml=%3CTOLData%3E%3Crealm%3Ecycle%3C%2Frealm%3E%3C%2FTOLData%3E';$xml = simplexml_load_file($btapi);echo $xml->PWC->ITEM[0]->CATEGORY_NAME;

Thank you! I am now able to display an individual value. What happens if I want to use this xml instead? http://api.traderonline.com/vehicles/queue...3C%2FTOLData%3EWhat would it look like to pull a photo from that? I'm just trying to comprehend how the paths work.. do you need to start with .$xml->I'm now experimenting with this code
echo "<strong>Category Name:</strong> ".$xml->PWC->ITEM[0]->CATEGORY_NAME."<br>";echo "<strong>Category ID:</strong> ".$xml->PWC->ITEM[0]->CATEGORY."<br>";echo "<strong>Type ID:</strong> ".$xml->PWC->ITEM[0]->TYPE."<br>";echo "<strong>Model:</strong> ".$xml->PWC->ITEM[0]->MODEL."<br>";

Link to comment
Share on other sites

I figured out how display a photo.. here's the existing code I'm using now:

<?php$btapi = 'http://api.traderonline.com/vehicles/queueSearch?xml=%3CTOLData%3E%3Crealm%3Ecycle%3C%2Frealm%3E%3CperPage%3E25%3C%2FperPage%3E%3Clatitude%3E36.847868%3C%2Flatitude%3E%3Clongitude%3E-76.292010%3C%2Flongitude%3E%3Ctype%3E356953%3C%2Ftype%3E%3Ccategory%3E-1%3C%2Fcategory%3E%3Cradius%3E25%3C%2Fradius%3E%3CmakeName%3EHONDA%3C%2FmakeName%3E%3Ccondition%3Eused%3C%2Fcondition%3E%3CminPrice%3E0%3C%2FminPrice%3E%3CmaxPrice%3E0%3C%2FmaxPrice%3E%3CminYear%3E0%3C%2FminYear%3E%3CmaxYear%3E0%3C%2FmaxYear%3E%3C%2FTOLData%3E';$xml = simplexml_load_file($btapi);$images = $xml->DATA->ADS->AD->DETAIL->PHOTO->IMAGE;foreach($xml as $test) {echo "<strong>Year:</strong> ".$xml->DATA->ADS->AD->DETAIL->YEAR."<br>";echo "<strong>Make:</strong> ".$xml->DATA->ADS->AD->DETAIL->MAKE."<br>";echo "<strong>Model:</strong> ".$xml->DATA->ADS->AD->DETAIL->MODEL."<br>";echo "<strong>Price:</strong> "."$".$xml->DATA->ADS->AD->DETAIL->PRICE."<br>";echo "<strong>Images:</strong>"."<br>";echo "<img src='$images'>"."<br><br>";}?>

and the output: http://65.213.231.100/~wizard/lauderdalema...om/xmldemo4.phpIt's working but only showing one unique listing. Can you tell me how to re-write it to show all the listings? Thank you again!

Link to comment
Share on other sites

Looping through $xml gets you nothing, since there is only one $xml object. $xml->DATA->ADS->AD is the object you need to iterate through. It's basically a list of every element called AD. Looping through it essentially counts off AD[0], AD[1], AD[2] and so on.

foreach ($xml->DATA->ADS->AD as $ad) {	echo "<strong>Year:</strong>"  . $ad->DETAIL->YEAR."<br>";	echo "<strong>Make:</strong>"  . $ad->DETAIL->MAKE."<br>";	echo "<strong>Model:</strong>" . $ad->DETAIL->MODEL."<br>";	echo "<strong>Price:</strong>" . "$" . $ad->DETAIL->PRICE."<br>";}

If you're just echoing the value of, say, $xml->DATA->ADS->AD->DETAIL->YEAR, you're always reading the data from AD[0], since your original loop isn't iterating through $xml->DATA->ADS->AD . Without an iterator or an index, $xml->DATA->ADS->AD always means "get the first AD."

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...