【问题标题】:similar function for http_post_fields?http_post_fields 的类似功能?
【发布时间】:2012-06-15 04:44:41
【问题描述】:

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 获取未定义的函数

【问题讨论】:

  • PEAR 不是扩展,它只是一个您可以自己下载的 php 库。与任何其他 .php 文件一样,您只需像处理另一个 .php 文件一样包含它们
  • @zerkms PECL_HTTP 是我需要的扩展,所以我不能包含它,pecl.php.net/package/pecl_http,我的主机只允许来自 PEAR 库的扩展,而 PECL_HTTP 不在这个库中,所以我需要一个类似的扩展与 PEAR 库中的 PECL_HTTP 具有相同的功能
  • "我的主机只允许来自 PEAR 库的扩展" --- PEAR 不是 php 扩展,实际上它们不需要像 PECL 那样“安装” . PEAR - 只是一个 php 库的存储库,您可以在没有提供者帮助的情况下复制和分发您的项目
  • @zerkms 好先生,我不确定你的意思,我的理解是 PEAR 是一个库,但我需要安装这个名为 PECL_HTTP 的扩展,当我搜索 pear.php.net 时找不到它;
  • 我明白这一点。我只是说 PEAR 不是扩展,您不需要托管服务提供商的帮助来使用 PEAR。 PS:关于答案-您可以以百万种方式发送帖子。 curl,套接字是最明显和可访问的,没有异国情调的pecl_http 扩展

标签: php pecl


【解决方案1】:

有很多方法可以从 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,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的代码示例。

【讨论】:

  • 这一行:curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 应该是:curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
【解决方案2】:

将种子上传到 torcache 我只是使用:

<?php
$upload_result = curl_upload('http://torcache.net/autoupload.php','torrent','/absoulte_full_path_to_torrent/torrent.torrent');

function curl_upload($url,$fileFormAttribute,$file){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        $post = array($fileFormAttribute=>"@".$file);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $response = curl_exec($ch);
        return $response;
}
?>

$upload_result 成功时会包含种子哈希,如果不是种子的绝对路径则会失败。

【讨论】:

  • 我在使用这个时得到一个空白网页,我尝试了 var_dump $upload_result 并得到 bool(false) 返回
猜你喜欢
  • 2012-12-15
  • 2012-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-19
  • 2011-02-28
  • 2012-10-04
相关资源
最近更新 更多