php - http_post_fields 的类似功能?

标签 php pecl

pecl_http 中的 http_post_fields 是否有类似的功能?我当前的主机只安装来自 http://pear.php.net/ 的扩展(不知道为什么,但我没有 ssh 访问权限,而是一个 web gui,并且只能安装可用的扩展。从那里)

这是我的代码

<?php
    $files = array(
        array(
            'name' => 'torrent',            // Don't change
            'type' => 'application/x-bittorrent',
            'file' => '0-273-70244-0.pdf.torrent'           // Full path for file to upload
        )
    );

    $http_resp = http_post_fields( 'http://torcache.net/autoupload.php', array(), $files );
    $tmp = explode( "\r\n", $http_resp );
    $infoHash = substr( $tmp[count( $tmp ) - 1], 0, 40 );
    var_dump($infoHash);
    unset( $tmp, $http_resp, $files );

目前这不起作用,因为我为 http_post_fields 获取了一个未定义的函数

最佳答案

有很多方法可以从 PHP 发布数据,这里有一些可以帮助您入门:

使用 stream context打开(使用 fopen )包含您要发送的帖子数据的 URL

function do_post($url, $data)
{
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));

  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}

代码示例改编自 Wez Furlong .

curl

使用CURL PHP 扩展将需要可用,这比现在更常见,但取决于您的主机。

function do_post($url, $data)
{
  $ch = curl_init($url);

  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  $response = curl_exec($ch);
  curl_close($ch);
  return $response;
}

代码示例改编自 Lorna Jane .

关于php - http_post_fields 的类似功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11044705/

相关文章:

centos - pecl 的 Varnish 安装在 whm 上出现错误

php - 从哪里获得 PHP 7.2 的 php_mcrypt.dll?

PHP Json 到 MySql (opencart)

php - $conn->query(...) 的结果是否可以为 0 条记录不为空

将 JSON 代码写入 txt 文件的 PHP 脚本缺少空格和换行符。怎么修?

php - 在 MAMP 上安装 ffmpeg

php - 已安装并加载 PECL 但未找到 http_negotiate_language

php - 使用 printer_write() 函数直接从 PHP 打印

使用 Ajax 将 JavaScript 变量传递给 PHP 不工作

php - Wordpress中的PHP函数不显示嵌入式YouTube视频