2010-10-15 51 views
1

我在php中創建了一個用於捕獲用戶屬性的腳本。php腳本中的ssl潛在問題

爲了做到這一點,它需要調用api來獲取這些屬性。

我設定的網址是:

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

然後使用的file_get_contents爲XML。

當我只是在瀏覽器中輸入這個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:[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'); 
} 

回答

0

您可以使用curl並允許您設置CURLOPT_FOLLOWLOCATION是真實的,這應該遵循任何重定向,並從最終頁返回輸出的curl_setopt

+0

感謝您的回覆。所以我實際上已經在使用捲曲腳本。我編輯了原始文章,以便看到代碼。你說我可以使用setopt,這應該工作? – Aaron 2010-10-15 14:27:37

+0

它應該,試試看,我相信這是一個更快的方法來確定它是否有效。 – 2010-10-15 14:58:15

+0

對不起,我想我問的原因是因爲我沒有看到任何不同。我將它添加到了curl_fetch函數的開頭。它是否有可能與xmlString = file_get_contents($ url)有關,因爲這是在curl之前完成的? – Aaron 2010-10-15 15:08:21