du94414 2017-07-03 14:46
浏览 47

PHP - 上传脚本中的神秘错误

i'm trying to upload a file to vshare.io trough their api. They're offering a php script to do so:

<?php
if(!function_exists('curl_init')) {
    die('CURL functions are not available. Debian: apt-get install php5-curl');
}

$file_path = ''; // Example: $file_path = '/home/files/file.exe';
$token = ''; // You can get your TOKEN from the following page  http://vshare.io/api.html
$post = array(
    'token'    => $token,
    'filesize' => filesize($file_path),
    'Filedata' => '@' . $file_path
);
// init
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://upload.vshare.io/upload_api.php');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:  "));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$output = json_decode($result, TRUE);
if(isset($output['upload'], $output['video'], $output['fileid']) && strlen($output['fileid']) == 7 && $output['upload'] == 1) {
    if($output['video'] == '1') {
        $file_type = 'video';
    } elseif($output['video'] == '0') {
        $file_type = 'file';
    }
    echo 'File Type: ' . $file_type . ' | File Link: http://vshare.io/d/' . $output['fileid'];
} else {
    echo 'Error: ' . $output['msg'];
}
?> 

i inserted my token and the correct filepath in Ubuntu, however when launching the script it will run for about 1 minute and then it will print "Error:" (last line of the script say to do so). No files are uploaded to my account

Any hint?

  • 写回答

1条回答 默认 最新

  • duanchensou8685 2017-07-03 14:50
    关注

    There could be 2 reasons. First is your user-agent, you have not set it. The most site today restricted BOT so you should set it for your CURL:

    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');

    The second reason is that the CURL process time out so you got nothing nor any message.

    Try this

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 400);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)