tags:

views:

57

answers:

1

Hello,

I'm trying to issue a call through API so that I can delete user properties. When I go to ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties it returns the XML which includes all of that users properties.

I'm trying to store that XML in variable $xmlString then I should be able to loop through to get the properties and in turn delete. The user properties is key value where the key and value can be anything so there is no way of being able to know all options unfortunately.

The code so far:

$delete = "http://www.ourwiki.com/@api/DELETE:users/$user_id/properties/%s";
$xmlString = file_get_contents('ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties')
$xml = new SimpleXMLElement($xmlString);

foreach($xml->property as $property) {
  $name = $property['name']; // the name is stored in the attribute
  file_get_contents(sprintf($delete, $name));
}

For some reason, file_get_contents does not seem to be able to get the XML. I've even made sure that allow_url_fopen is enabled in the environment. Any help with this would be greatly appreciated.

A: 

Try

$xmlString = file_get_contents("http://ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties");

in the second line. By not including the protocol at the beginning, PHP is looking for a file in the servers filesystem.

EDIT:

You said var_dump($xmlString) returns false. From the docs:

This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to maxlen bytes. On failure, file_get_contents() will return FALSE.

Which means PHP cannot GET any data from that URL.

Try using

$xmlString = file_get_contents(urlencode('http://ourwiki.com/@api/users/[email protected]/properties'));
W_P
Sorry, when I wrote that I omitted it in the post but I did include the protocol when actually testing the script. Doesn't seem to really have an effect though.
Aaron
what is the result of the `var_dump` that VolkerK asked about?
W_P
I just added a comment above, but it is returning bool(false)
Aaron
Clearly it's unable to get it. That's why I posted the question. I'm able to get the XML without a problem when using the browser. I've also ensured that allow_url_fopen is enabled through php.ini. Is there something else you know of I can check? Or is this just impossible?
Aaron
See last edit. Turn the `%40` back into a `@` character, and run the whole URL through `urlencode`.
W_P
Sorry I missed that, still appears to be returning bool(false) :(
Aaron
Not that this matter but I was wondering if there is a way on the server to see if I can even get to that URL. See if it's resolving. I was thinking wget but not sure if that can handle the XML.
Aaron
cURL is a good command line tool for that. here is a link to a quick example of transmitting XML with cURL:http://snippets.dzone.com/posts/show/181. and here are the cURL docs: http://curl.haxx.se/docs/
W_P
That's pretty neat. Well it looks like it's able to find the page. It's giving me an SSL cert doesn't match target host name but I don't think thats our problem with file_get_contents
Aaron
oh, so the api is actually using `https`? that must be the problem...
W_P
It appears that there is a redirect but either way, yes it looks to be using https. So it only works when using http?
Aaron
well, if it is redirecting to https, does it require sending credentials?
W_P
I believe its just a rewrite within apache which doesn't require additional credentials other than the ssl cert itself.
Aaron
is there a way to do this easily with curl? i've heard that it works better when it comes to parsing xml.
Aaron
I've never tried _parsing_ XML with curl, I don't even know if it can parse...
W_P
Actually, I managed to get much further on my VM! It was giving me an failed to open stream, so by giving it the credentials it allowed file_get_contents to work. Now it is telling me, "Warning: sprintf() [function.sprintf]: Too few arguments in /var/www/script.php on line 18" What am I missing here?
Aaron
We might need to know what's on line 18 to figure that out ;)
Rudu
are there any `name`s that have a `%` character in them?
W_P