简体   繁体   中英

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.

Actually it looks like the script is working, because I am getting a URL and a name as result. 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 Error: Error Domain=Parse Code=150 "The operation couldn't be completed. (Parse error 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

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'
    ),
 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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