【问题标题】:Use curl in php script在 php 脚本中使用 curl
【发布时间】:2011-04-05 22:06:06
【问题描述】:

我正在尝试创建一个脚本,该脚本将删除特定个人的所有用户属性。我可以使用 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 响应标头。对调试很有用。

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

更新:

<?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 xml curl


    【解决方案1】:

    您可以使用php curl,或使用exec 进行卷曲。

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

    在php-curl中设置delete方法做:

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

    edit2

    将上面的代码粘贴到函数中:

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

    完成。

    【讨论】:

    • 当我运行 phpinfo 时,似乎启用了 curl。这是否意味着我可以简单地在 php 脚本中运行该命令。如果是这样,那实际上是如何完成的?不幸的是,我对 curl 不是很熟悉。
    • 网址和凭据属于哪里?我如何实际调用 delete 方法?
    • 非常感谢!这很有帮助。这是否意味着我需要在 for 循环中调用 $ch 而不是 $delete?
    • @Aaron 您需要将我发布的整个代码块放入循环中。
    • 好吧,我以为是这样。我只是不确定如何处理我现有的删除。因为它在 file_get_contents 中,并且将使用当前的 api“删除”调用而不是 curl 调用。我还需要进行任何其他更改吗?
    【解决方案2】:

    【讨论】:

      【解决方案3】:

      看起来你正在寻找php中的curl函数调用,包括curl_setopt

      【讨论】:

      • 我查看了 curl_setopt,我只是不确定如何根据我的示例运行命令。比如我如何提供 URL 和凭据?
      • 谢谢,你知道我应该如何正确调用删除吗?截至目前,我的 for 循环正在使用旧的 api 调用 $delete。我只是不确定如何使用他在我的示例中提供的代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 2018-03-10
      • 2012-08-30
      相关资源
      最近更新 更多