tags:

views:

34

answers:

1

I've created a script in php that is used to capture the properties for users.

In order to do so, it requires calling the api to obtain those properties.

The url I set is:

 $url=("http://user:[email protected]/@api/users/=$user_id/properties");

Then use file_get_contents for the xml.

When I simply type this url into the browser it works fine. It immediately output those properties for the given user. However it looks as if it automatically switches it to https. Is there something that needs to be done so this can work when using php?

Code:

<?php

$user=$_GET['userid'];
$user_id=str_replace(array('@', '#'), array('%40', '%23'), $user);

print "User-id: $user";
print "<br /><br />";

$url=("http://user:[email protected]/@api/users/=$user_id/properties");
echo $url;
$xmlString=file_get_contents($url);

$delete = "http://user:[email protected]/@api/users/=$user_id/properties/";
$xml = new SimpleXMLElement($xmlString);

function curl_fetch($url,$username,$password,$method='DELETE')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch,CURLOPT_USERPWD,"$username:$password");
    return  curl_exec($ch);
}

print "The following properties have been removed: ";
print "<br />";

if(!count($xml->property)) die('No properties exist for this user');

foreach($xml->property as $property) {
  $name = $property['name'];
  $name2=str_replace(array('@', '#'), array('%40', '%23'), $name);
  print $name2;
  print "<br />";
  curl_fetch($delete . $name2,'user','pass');
}
A: 

You can use curl and the curl_setopt which allows you to set the CURLOPT_FOLLOWLOCATION to be true, this should follow any redirects and return the output from the ending page.

Brad F Jacobs
Thanks for replying. So i'm actually already using curl the script. I edited the original post so you could see code. You're saying I can use the setopt and this should work?
Aaron
It should, give it a try and see, I am sure that is a much quicker way of finding out if it works or not.
Brad F Jacobs
Sorry, I guess the reason I asked is because I didn't see anything different. I added it to the beginning the of curl_fetch function. Is it possible that its something with the xmlString=file_get_contents($url) since this is done prior to curl?
Aaron