【问题标题】:Silent download with POST parameters over http/https with php使用 php 通过 http/https 使用 POST 参数进行静默下载
【发布时间】:2014-07-16 01:56:47
【问题描述】:

我有一个 php 脚本,需要通过 http/https 下载文件并为请求指定 POST 参数。

应该没有浏览器弹出窗口,只是静默下载,例如到 ~/. 不幸的是,包装 wget 是不允许的解决方案。

有什么简单的方法吗?

【问题讨论】:

  • 如果启用了 curl,你可以使用它吗?

标签: php http post https


【解决方案1】:

你可以使用:

  1. file_get_contents() 函数 — IMO 通过 HTTP(或 HTTPS)获取(或 POST)简单内容的最简单方法。用法示例:

    <?php
    $opts = array('http' => array(
        'method'  => 'POST',
        'content' => $body, // your x-www-form-urlencoded POST payload
        'timeout' => 60,
    ));
    $context  = stream_context_create($opts);
    $result = file_get_contents($url, false, $context, -1, 40000);
    
  2. CURL — 发送 POST 请求的另一种简单方法。非常基本的代码示例:

    <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    // $body is your x-www-form-urlencoded POST payload
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);
    curl_close ($ch);
    
  3. 您拥有或可以下载的任何其他 PHP HTTP 客户端(Zend_Http_Client、HTTP_Client、Whatever_Client)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-19
    • 2012-07-16
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多