php - 在 php 脚本中使用 curl

标签 php xml curl

我正在尝试创建一个脚本来删除特定个人的所有用户属性。我可以使用 api 调用来获取用户的属性。我正在尝试使用删除 api 来删除每个属性。但我这样做有问题。下面是代码:

$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));
}

我相信我需要使用 curl 来执行实际的删除操作。这是该命令的示例 (property=something):

curl -u username:password -X DELETE -i http://ourwiki.com/@api/users/=john_smith@ourwiki.com/properties/something

-u 提供外部用户身份验证。

-X 指定HTTP请求方法。

-i 输出 HTTP 响应 header 。用于调试。

我可以将其合并到现有脚本中吗?或者还有什么我需要做的吗?任何帮助将不胜感激。

更新:

<?php

$user_id="john_smith@ourwiki.com";

$url=('http://aaron:12345@192.168.245.133/@api/deki/users/=john_smith@ourwiki.com/properties');
$xmlString=file_get_contents($url);

$delete = "http://aaron:12345@192.168.245.133/@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');
}

?>

最佳答案

您可以使用 php curl ,或使用 exec 进行 curl .

如果您的 Web 服务器上已经启用了 curl,请使用 php curl。如果您无法安装 php-curl,请复制一个命令行版本的 curl,您就可以开始了。

在 php-curl 中设置删除方法:

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

编辑

像这样:

$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);

编辑2

把上面的代码放在一个函数中:

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);
}

并用新函数替换脚本中对 file_get_contents() 的调用。

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

完成。

关于php - 在 php 脚本中使用 curl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3603286/

相关文章:

c++ - 有没有办法修改样式表,以便将带有空标签的 XML 文档转换为 <tag/>?

C++ Curl 后动态变量

java - Delta XML-Dita 比较捕获 com.deltaxml.dita.FilterProcessingException

xml - 设置我自己的 XML 命名空间?

java - 无法反序列化 `java.util.ArrayList` 的实例

php - 上传图片时如何防止提交空表单?

php - 迁移到 https 后 curl 返回 301 错误

c# - 使用安全证书使用 .NET 进行 cURL 请求

php - php 和 mysql 中的奇怪错误

php - 编写一个程序,用函数的答案填充数据库