静默下载与POST参数通过http/https与php


Silent download with POST parameters over http/https with php

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

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

有什么简单的方法可以做到吗?

您可以使用:

  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),你有或可以下载。