简体   繁体   English

Parse.com-使用REST API和PHP上传图片

[英]Parse.com - Upload image using REST API and PHP

I created a PHP-script which is uploading an image, picked up from an HTML-form, to the Parse.com backend. 我创建了一个PHP脚本,该脚本将从HTML表单中提取的图像上传到Parse.com后端。

Actually it looks like the script is working, because I am getting a URL and a name as result. 实际上看起来脚本正在运行,因为我得到的是URL和名称。 After the Upload I am associating the uploaded file with an Object. 上传之后,我将上传的文件与对象相关联。 The result is also looking fine and the image is appearing inside the data browse. 结果也看起来不错,并且图像出现在数据浏览器中。

But if I try to get the image in my iOS app or take a look at it inside of the browser (by clicking on it) I only get an access denied alert or a white page (with broken image icon). 但是,如果我尝试在iOS应用中获取图像或在浏览器中查看图像(通过单击它),则只会收到拒绝访问警报或白页(图像图标已损坏)。

iOS Error: Error Domain=Parse Code=150 "The operation couldn't be completed. (Parse error 150.) iOS错误:错误域=分析代码= 150“该操作无法完成。(分析错误150。)

Here you can see my code: 在这里您可以看到我的代码:

Upload image: 上传图片:

$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);

Associating image to Object (image name for test case hardcoded) 将图像与对象关联 (测试用例的图像名称已硬编码)

$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);

Image Url , which I am getting back from Parse: http://files.parse.com/725d8f61-de18-4de5-a84c-dcc6e74c43ae/197e7bb6-62ad-4dc4-a011-6db88333ac45-BMW_1series_3door_Wallpaper_1920x1200_01.jpg 我从Parse返回的图片网址http : //files.parse.com/725d8f61-de18-4de5-a84c-dcc6e74c43ae/197e7bb6-62ad-4dc4-a011-6db88333ac45-BMW_1series_3door_Wallpaper_1920x1200_01.jpg

Data Browser Screenshot : 数据浏览器截图

解析数据浏览器截图

I think you need to specify the url as well 我认为您也需要指定网址

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM