Jump to content

Saving the contents of an external XML file to a variable


Saphod

Recommended Posts

Hi,

 

I would like to use the digg.com API to analyze the most recent "Diggs" of a certain URI.

 

Digg.com provides an XML-file under a special URI.

 

The following URI provides e.g. the latest 10 "diggs" for the domain "heise.de" in descending order, highest count first:

 

http://services.digg.com/stories/?domain=heise.de&appkey=http%3A%2F%2Fexample.com%2fapplication&sort=digg_count-desc&count=10

 

The result may look like this:

[pre]

<stories timestamp="1196078642" total="216" offset="0" count="10">

  <story id="1173061" link="http://www.heise.de/tp/r4/artikel/5/5263/1.html" submit_date="1168676669" diggs="3627" comments="263" promote_date="1168747803" status="popular" href="http://digg.com/security/How_the_US_National_Security_Agency_access_was_built_into_Windows">

    <title>

        How the US National Security Agency access was built into Windows

    </title>

    <description>

        A careless mistake by Microsoft programmers had revealed that special access codes prepared by the US NSA have been secretly built into Windows. The NSA access system was built into every version of the Windows operating system in use. The discovery came close on the heels of the revelations that Lotus, had built the same thing in its Notes program

    </description>

    <user name="nstanosheck" icon="http://digg.com/users/nstanosheck/l.png" registered="1142722132" profileviews="10295"/>

    <topic name="Security" short_name="security"/>

    <container name="Technology" short_name="technology"/>

  </story>

 

  <story id="....next story...">...</story>

 

</stories>

[/pre]

 

I would like to extract some information like the count and the title to present this as a <ul>-list.

 

Problem:

how do I get the contents of the XML-file stored in a variable, including the tags?

 

I have tried the Snoopy() class that simulates a browser - which did not show me the tags but only the text without tags.

Then I have tried fopen() and fsockopen(), but PHP returns a "403 Forbidden".

 

How can I do something like:

$contents = ... get the contents of the XML-file including tags which is provided by this URI ... ???

 

Then I could try to use expat to parse the contents (see http://php.net/xml).

 

Thanks in advance.

Link to comment
Share on other sites

Did not work... I got this error message:

[pre]Warning:

file_get_contents(http://services.digg.com/stories/?domain=heise.de&appkey=http%3A%2F%2Fexample.com%2fapplication&sort=digg_count-desc&count=10)

[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden[/pre]

Hmm... comparing the way I used fsockopen() to a script where it actually works, I think I might have used it wrong... I took the whole URI, not only the domain. Maybe someone can help me out with fsockopen() and fputs() and all that, because I have never used it before...

 

Thanks!

 

[update]

Is it possible that my provider does not allow to open external files via that method? I have read that in a different forum where they recommend to use sockets instead. It would be nice if someone could help me out with that...

Link to comment
Share on other sites

I have now tried this:

 

(...)
<?php
$fp = fsockopen("http://services.digg.com", "80", $errno, $errstr);

if (!$fp) { echo "Error!"; }
else {
      fwrite($fp, "GET stories/?domain=heise.de&appkey=http%3A%2F%2Fexample.com%2fapplication&sort=digg_count-desc&count=10 HTTP/1.1\r\n");
      fwrite($fp, "Host: services.digg.com\r\n");

      while (!feof($fp)) {
         $d = fgets ($fp,2048);
         echo $d;
       }
fclose($fp);
}
?>
(...)

 

which leads to this:

[pre]

Warning: fsockopen() [function.fsockopen]:

php_network_getaddresses: getaddrinfo failed:

Name or service not known in ....../test.php on line 14

 

Warning: fsockopen() [function.fsockopen]:

unable to connect to http://services.digg.com:80 in ....../test.php on line 14

Error!

[/pre]

I found something interesting here, maybe I will use that:

http://groups.google.com/group/diggapi/...

 

The funny thing is: there is a Wordpress plugin that actually uses fsockopen() quite successfully...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.