通过 API 使用 PHP 和 cURL 将文件上传到 RapidShare
问题描述:
我正在尝试使用带有 cURL 的 API 和 PHP 将文件上传到 RapidShare.到目前为止,我有这个代码:
I'm trying to upload files to RapidShare using their API and PHP with cURL. So far, I've got this code:
// Get the RapidShare server to upload to
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$uploadServer = curl_exec($ch);
// Upload the file to RapidShare
$url = 'http://rs' . $uploadServer . '.rapidshare.com/cgi-bin/rsapi.cgi?sub=upload';
$url .= '&login=login';
$url .= '&password=mypass';
$url .= '&filename=' . $filename;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
// Set post fields
$postFields = array('filecontent' => array('@' . $targetDir . '/' . $id));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$resp = curl_exec($ch);
die($resp);
但我得到的唯一响应是:错误:子程序无效.(b6ba5d82)
.如果我只是这样做(基本上不要随请求发送文件):
But the only response I get is: ERROR: Subroutine invalid. (b6ba5d82)
. If I just do this (basically don't send the file with the request):
// Upload the file to RapidShare
$url = 'http://rs' . $uploadServer . '.rapidshare.com/cgi-bin/rsapi.cgi?sub=upload';
$url .= '&login=login';
$url .= '&password=mypass';
$url .= '&filename=' . $filename;
curl_setopt($ch, CURLOPT_URL, $url);
$resp = curl_exec($ch);
我收到此响应:错误:未传输文件.(f269d341)
所以我猜我通过 POST 发送文件的方式有问题.
So I'm guessing there's something wrong with the way I'm sending the file via POST.
有人知道哪里出了问题吗?
Anyone know what could be wrong?
谢谢.
答
添加 curl 参数
curl_setopt($ch, CURLOPT_INFILESIZE,(string)filesize($filename));
curl_setopt($ch, CURLOPT_INFILE,fopen($filename,'r'));
和 $filename 必须写完整路径.对不起,我的英语很少.
and $filename must write full path. Sorry Im english very little.