http_post_fields的功能类似?

时间:2012-06-15 04:44:42

标签: php pecl

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

这是我的代码

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

2 个答案:

答案 0 :(得分:3)

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

<强>流

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

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

答案 1 :(得分: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将包含torrent哈希,如果它不是torrent的绝对路径,它将失败。