我在 php 中创建了一个脚本,用于捕获用户的属性。
为此,需要调用 api 来获取这些属性。
我设置的url是:
$url=("http://user:pass@oursite.com/@api/users/=$user_id/properties");
然后对 xml 使用 file_get_contents。
当我在浏览器中简单地输入这个 url 时,它工作正常。它立即为给定用户输出这些属性。但是看起来好像它会自动将其切换到 https。是否需要做些什么才能在使用 php 时正常工作?
代码:
<?php
$user=$_GET['userid'];
$user_id=str_replace(array('@', '#'), array('%40', '%23'), $user);
print "User-id: $user";
print "<br /><br />";
$url=("http://user:admin@oursite.com/@api/users/=$user_id/properties");
echo $url;
$xmlString=file_get_contents($url);
$delete = "http://user:admin@oursite.com/@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');
}
最佳答案
您可以使用 curl和 curl_setopt这允许您将 CURLOPT_FOLLOWLOCATION
设置为 true,这应该遵循任何重定向并返回结束页面的输出。
关于php - php 脚本中 ssl 的潜在问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3942417/