2010-08-30 5 views
4

J'essaie de créer un script qui va supprimer toutes les propriétés de l'utilisateur pour un individu particulier. Je suis capable d'utiliser un appel api pour obtenir les propriétés des utilisateurs. Et j'essaye d'employer une api de suppression pour enlever chaque propriété. Mais j'ai un problème en train de le faire. Voici le code:Utiliser curl dans le script php

$delete = "http://www.ourwiki.com/@api/DELETE:users/$user_id/properties/%s"; 
$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)); 
} 

Je crois que j'ai besoin d'utiliser curl pour effectuer la suppression réelle. Voici un exemple de cette commande (property = something):

curl -u username:password -X DELETE -i http://ourwiki.com/@api/users/[email protected]/properties/something 

-u Fournit une authentification utilisateur externe.

-X Spécifie la méthode de requête HTTP.

-i Affiche les en-têtes de réponse HTTP. Utile pour le débogage.

Est-ce quelque chose que je peux intégrer directement dans le script existant? Ou y a-t-il autre chose que je dois faire? Toute aide serait grandement appréciée.

mise à jour:

<?php 

$user_id="[email protected]"; 

$url=('http://aaron:[email protected]/@api/deki/users/[email protected]/properties'); 
$xmlString=file_get_contents($url); 

$delete = "http://aaron:[email protected]/@api/deki/DELETE:users/$user_id/properties/%s"; 
$xml = new SimpleXMLElement($xmlString); 

function curl_fetch($url,$username,$password,$method='DELETE') 
{ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it 
    curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this 
    return curl_exec($ch); 
} 

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

?> 

Répondre

2

Vous pouvez utiliser php curl ou shell à friser en utilisant exec.

Si curl est déjà activé sur votre serveur Web, utilisez php curl. Si vous ne pouvez pas installer php-curl copier une version en ligne de commande de curl et vous êtes prêt à partir.

en php-boucle pour définir la méthode de suppression do:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');

modifier

Quelque chose comme ceci:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://www.ourwiki.com/@api/whatever/url/you/want/or/need"); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it 
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this 
$output = curl_exec($ch); 

Edit2

bâton qui code ci-dessus en fonction:

function curl_fetch($url,$username,$password,$method='DELETE') 
{ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it 
    curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this 
    return curl_exec($ch); 
} 

et remplacer l'appel à file_get_contents() dans votre script avec la nouvelle fonction.

curl_fetch(sprintf($delete, $name),'aaron','12345');

Terminé.

+0

Quand je lance phpinfo il semble que boucle est activée. Cela signifie donc que je peux simplement exécuter cette commande dans le script PHP. Si oui, comment cela se passe-t-il? Je ne suis pas très familier avec curl malheureusement. – Aaron

+0

D'où viennent l'url et les informations d'identification? Et comment puis-je appeler la méthode delete? – Aaron

+0

Merci beaucoup! Cela aide beaucoup. Cela signifie-t-il que j'ai besoin d'appeler $ ch au lieu de $ delete dans la boucle for? – Aaron

0

semble que vous cherchez la fonction en php boucle appels, y compris curl_setopt

+0

J'ai regardé curl_setopt, je ne suis pas sûr exactement comment exécuter la commande basée sur mon exemple. Dois-je fournir l'URL et les informations d'identification? – Aaron

+0

Byron a fait un bon travail décrivant – Zak

+0

Merci, toute chance que vous savez comment je voudrais appeler correctement supprimer? À partir de maintenant ma boucle for utilise l'ancien appel api pour $ supprimer. Je ne sais pas exactement comment utiliser le code qu'il a fourni dans mon exemple. – Aaron