【问题标题】:Parse.com - Upload image using REST API and PHPParse.com - 使用 REST API 和 PHP 上传图片
【发布时间】:2014-02-23 19:31:56
【问题描述】:

我创建了一个 PHP 脚本,它将从 HTML 表单中提取的图像上传到 Parse.com 后端。

实际上,脚本似乎正在运行,因为我得到了一个 URL 和一个名称作为结果。 上传后,我将上传的文件与一个对象相关联。 结果看起来也很好,并且图像出现在数据浏览器中。

但是,如果我尝试在我的 iOS 应用程序中获取图像或在浏览器中查看它(通过单击它),我只会收到拒绝访问警报或白页(带有损坏的图像图标)。

iOS 错误: Error Domain=Parse Code=150 “操作无法完成。(解析错误 150。)

在这里你可以看到我的代码:

上传图片:

$teamImage = $_FILES["teamImage"];

$APPLICATION_ID = "XXXXXXXXXXXXXXXXXXX";
$REST_API_KEY = "XXXXXXXXXXXXXXXXXXX";

$urlFile = 'https://api.parse.com/1/files/' . $teamImage['name'];
$image = $teamImage['tmp_name'];

$headerFile = array(
    'X-Parse-Application-Id: ' . $APPLICATION_ID,
    'X-Parse-REST-API-Key: ' . $REST_API_KEY,
    'Content-Type: ' . $teamImage['type'],
    'Content-Length: ' . strlen($image),
);

$curlFile = curl_init($urlFile);
curl_setopt($curlFile, CURLOPT_POST, 1);
curl_setopt($curlFile, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlFile, CURLOPT_POSTFIELDS, $image);
curl_setopt($curlFile, CURLOPT_HTTPHEADER, $headerFile);
curl_setopt($curlFile, CURLOPT_SSL_VERIFYPEER, false); 

$responseFile = curl_exec($curlFile);
$httpCodeFile = curl_getinfo($curlFile, CURLINFO_HTTP_CODE);

$result = array('code'=>$httpCodeFile, 'response'=>$responseFile);

将图像与对象关联(硬编码测试用例的图像名称)

$url = 'https://api.parse.com/1/classes/Teams';
$data = array(
    'name' => 'Test',
    'teamImage' => array(
        'name' => '......jpg',
        '__type' => 'File'
        ),
    );
$_data = json_encode($data);
$headers = array(
    'X-Parse-Application-Id: ' . $APPLICATION_ID,
    'X-Parse-REST-API-Key: ' . $REST_API_KEY,
    'Content-Type: application/json',
    'Content-Length: ' . strlen($_data),
);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$responseTwo = curl_exec($curl);
$httpCodeTwo = curl_getinfo($curl, CURLINFO_HTTP_CODE);

$resultTwo = array('code'=>$httpCodeTwo, 'response'=>$responseTwo);

图片网址,我从 Parse 中返回:http://files.parse.com/725d8f61-de18-4de5-a84c-dcc6e74c43ae/197e7bb6-62ad-4dc4-a011-6db88333ac45-BMW_1series_3door_Wallpaper_1920x1200_01.jpg

数据浏览器截图

【问题讨论】:

    标签: php image rest parse-platform


    【解决方案1】:

    我认为您还需要指定网址

     $data = array(
       'name' => 'Test',
       'teamImage' => array(
          'name' => '......jpg',
          '__type' => 'File',
          'url' => '....jpg'
        ),
     );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      相关资源
      最近更新 更多