Search code examples
phprestcurlquickblox

QuickBlox - How to use REST API + PHP to create blob content


I tried to create blob content (an image) with QuickBlox REST API(curl) and PHP, the response return always says "size":null, and image is not uploaded to QuickBlox backend, i.e. shows "Not uploaded" in QuickBlox admin portal.

I guess maybe the file name is not properly passed to the API, but the QuickBlox REST API document is too simple and I couldn't figure out.

Below is my code, much appreciated if anyone could help, many thanks:

JS:

function upload_profile_photo()
{
    var data = new FormData();
    jQuery.each(jQuery('.editableform input[name=avatar]')[0].files, function(i, file) {
        data.append('file-'+i, file);
    });
    jQuery.ajax({
        url: 'update_profile_photo.php',
        data: data,
        cache: false,
        contentType: false,
        processData: false,
        type: 'POST',
        success: function(result){
            showSuccess(result);
        }
    });
}

update_profile_photo.php:

...
    $imageName = $_FILES['file-0']['tmp_name']; // Tried ['name'] also failed
    $imageType = $_FILES['file-0']['type'];
...
$response = UpdateProfileImage($imageType, $imageName);
...
function UpdateProfileImage($imageType, $imageName) 
{
    $requestBody = http_build_query(array(
                    'blob' => array(
                            'content_type' => $imageType,
                            'name' => $imageName
                            )
            ));
    ...
    $response = $this->Curl_Post($requestHeader, $requestName, $requestBody);
    return $response;

}

QuickBlox Response:

{"blob":{"id":1159901,"uid":"d328c47565614cbdaed9671ce7bc6d8000",
"content_type":"image/jpeg","name":"/tmp/phpbRqnXb","size":null, ...}

Solution

  • To upload a file to QuickBlox you have to do 3 requests as mentioned here:

    http://quickblox.com/developers/Content#Typical_use_.D1.81ases

    1. Create a file
    2. Upload a file
    3. Declaring file uploaded

    After this your file will be completely uploaded to the QuickBlox backend and you will be able to check it in Admin panel