Jump to content

Domdocument Help


weep

Recommended Posts

Hi guys,

 

I am trying to grab some data from an API by using cURL and DOMDocuments. This is what I have:

 

$username="XXXXX";
$password="XXXX";
$base_url="https://xxxxxx.net/api/";
$href_url="resources/120529-0039/workorders";
$ch = curl_init($base_url . $href_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept"=>"application/json"));
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);

 

So far so good, at this point if I echo the $result, and look at the source, I would get:

 

<?xml version="1.0" encoding="utf-8"?>
<References xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" p3:href="resources/120529-0039/workorders" xmlns:p3="http://www.w3.org/1999/xlink" xmlns="http://xxxxxx.net/Apps/20090225/Entities">
<Link p3:href="workorders/3012-23" p3:title="Brandlarmsprov(mar-dec)" />
<Link p3:href="workorders/3021-27" p3:title="Reservkraft(2:a torsdagen varje månad)" />
<Link p3:href="workorders/4375-1" p3:title="Uppdatering av IBK ritningar efter heltäckande brandlarm." />
</References>

 

 

I am after the "workorders/3012-23" part. Here it all falls aparat for me, I have been playing around with:

 


$husdjur = new DOMDocument();
$husdjur->loadXML($result);
$href = $husdjur->getElementsByTagName('Link')->item(0)->getAttribute('p3:href');

 

And was able to populate $href with one value, but I want them all. I need the following:

 

$href[0] = "workorders/3012-23"

$href[1] = "workorders/3021-27"

$href[2] = "workorders/4375-1"

and so on...

 

Any thoughts? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/269929-domdocument-help/
Share on other sites

  • 1 month later...

Great. Please post your findings such that others can learn from your mistakes.

 

Sweet jesus, I completely forgot about this thread! Here it goes, if anyone cares:

 

$username="XXXXX";
$password="XXXX";
$base_url="https://xxxxxx.net/api/";
$href_url="resources/120529-0039/workorders";
$ch = curl_init($base_url . $href_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept"=>"application/json"));
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);

$husdjur = new DOMDocument();
$husdjur->loadXML($result);

$i = 0;
$end = 0;
$ArendeId = array();

while($end < 1)
{
$href = $husdjur->getElementsByTagName('Link')->item($i);

if($href == ""){		
$end = 1;
}else{

$href2[$i] = $href->getAttribute('p3:href');
echo "<br>", $href2[$i];

}
$i++;
}

 

I guarantee that there are better ways of doing this, but this is how I solved it.

Link to comment
https://forums.phpfreaks.com/topic/269929-domdocument-help/#findComment-1397156
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.