使用PHP中的cURL将文件上载到解析

时间:2016-01-17 07:35:28

标签: php curl parse-platform

我一直在尝试使用PHP中的cURL为服务器上传jpg到我的Parse.com应用程序。文件已正确创建,但不包含正确的数据......简而言之,图像已损坏 这是我的代码

<?php

header('Content-Type: application/json');
$ParseAppID = "X-Parse-Application-Id: ..." ;
$ParseRestKey = "X-Parse-REST-API-Key: ..." ;
$imageContent = "Content-Type: image/jpeg";
$mimetypeImage = 'image/jpeg';
$POST = "POST";

if($_SERVER['REQUEST_METHOD']=='POST'){


    if (isset($_POST['text']) && isset($_POST['image'])) {
        $text = $_POST['text'];
        $image = $_POST['image'];

        $binary = base64_decode($image);
        $filename = "file.jpg";
        $file2 = "/public_html/VolleyTest/file.jpg";
        file_put_contents($filename, $binary);
        $postname = 'name';

        $cfile = new CURLFile($file2,$mimetypeImage,$postname);
        $data = array($cfile);

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $POST);
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($curl, CURLOPT_HEADER, FALSE);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array($ParseAppID, $ParseRestKey, $imageContent));
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_URL, "https://api.parse.com/1/files/$filename");

        $result = curl_exec( $curl );

        curl_close( $curl );

        $result = json_decode($result);
        echo  $result->name;
        echo $result->url;
        echo json_encode($result);


    }
//Response -> Error
    else{

        $response["success"] = 0;
        $response["message"] = "Improper request";
        header('Content-Type: application/json');
        // echoing JSON response
        echo json_encode($response, JSON_PRETTY_PRINT);
    }
}
//Response -> Error

else{

    $response["success"] = -1;
    $response["message"] = "Improper request no POST request found";
    header('Content-Type: application/json');
    // echoing JSON response
    echo json_encode($response, JSON_PRETTY_PRINT);

}
?>

我尝试了很多,但每次我得到一张空图像......请帮忙

0 个答案:

没有答案