为什么ConvertAPI无法使用PHP库工作

时间:2018-10-23 13:20:55

标签: php converters

我正在使用PHP库[ConvertAPI]中的ConvertAPI

昨天我的代码在ConvertAPI上正常工作,但今天突然不起作用,并给我错误操作在0毫秒后超时,收到0字节中的0字节。我没有更改任何一行代码,但是我不知道为什么它会出错。请帮助我认识这个问题的人。

我的代码如下:

<!DOCTYPE html>
<html>
  <head>
    <title>PHP - Convert API Example</title>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  </head>
  <body>
    <div class="container">
      <form method="POST" action="" enctype="multipart/form-data">
        <div class="form-group">
            <label>Add Image:</label>
            <input type="file" name="file[]" class="form-control" multiple="multiple">
        </div>
        <div class="form-group">
            <button class="btn btn-success">Submit</button>
        </div>
     </form>
    </div>
 </body>
</html>

<?php 
require 'vendor/autoload.php';
use \ConvertApi\ConvertApi;

ConvertApi::setApiSecret('My-secret-key');

if(!empty($_FILES["file"])){
   try {
    if(count($_FILES['file']['name']) > 0){

       $newFileName = $_FILES['file']['name'];
       //Loop through each file
       for($i=0; $i<count($_FILES['file']['name']); $i++) {
       //Get the temp file path
          $tmpFilePath = $_FILES['file']['tmp_name'][$i];
          $fileName = $_FILES['file']['name'][$i];
          $newFileName = substr($fileName, 0 , (strrpos($fileName, ".")));      
         //Make sure we have a filepath
          if($tmpFilePath != ""){   

              //save the filename
              $shortname = $_FILES['file']['name'][$i];
              //save the url and the file
              $filePath = "uploads/".$_FILES['file']['name'][$i];
              //Upload the file into the temp dir
              if(move_uploaded_file($tmpFilePath, $filePath)) {
                 $files[] = $shortname;               
              }
              $result = ConvertApi::convert('jpg', ['File' => $filePath]);
              $final_result = $result->response['Files'][0]['FileName'];
            //echo '<pre>'; print_r($result);
             $result->getFile()->save('uploads/'.$newFileName.'.jpg');        
           }
     }
  }

}
catch (Exception $e) {
  echo "Something else went wrong: " . $e->getMessage() . "\n";
}
}

为此,我使用以下方法安装了PHP库

撰写者需要convertapi / convertapi-php

1 个答案:

答案 0 :(得分:0)

这在Linux上对我有用。 编辑文件:

{
  "Dictionary": {
    "public": "abcd"
  },
  "Inner": {
    "Dictionary": {
      "private": "abcd"
    }
  }
}

并粘贴以下内容:

sudo /etc/resolv.conf 

将使用OpenDNS,注释掉文件中的现有名称储备