2

Currently using this, but file that is stored is empty, so I supose no data is going through.

$post = array("file"=>'@'.$_FILES['uploadfile']['tmp_name']);         
$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id : XXXXXXXXXXXXXX','X-Parse-REST-API-Key: XXXXXXXXx', 'Content-type: image/jpeg'));
curl_setopt($ch, CURLOPT_URL, 'https://api.parse.com/1/files/'.$_FILES['uploadfile']['name']);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;

2 Answers 2

7

Got it working. You need to send the binary data as the post field. duh. Before this you should probably create some restrictions (file size, type etc)

$post = file_get_contents($_FILES['uploadfile']['tmp_name']);

$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id : XXXXXXXX','X-Parse-REST-API-Key: XXXXXXXXXXXXXXX', 'Content-type: image/jpeg'));
curl_setopt($ch, CURLOPT_URL, 'https://api.parse.com/1/files/'.$_FILES['uploadfile']['name']);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
Sign up to request clarification or add additional context in comments.

Comments

1

Use this work perfectly same way as we need.....

function sendImageToParse($url, $filename) {
    $this->layout = FALSE;
    $this->autoRender = false;

    $headers = array(
        'X-Parse-Application-Id:' . $this->APPLICATION_ID,
        'X-Parse-REST-API-Key:' . $this->REST_API_KEY,
        'Content-Type: image/jpeg'
    );

    $data = file_get_contents($url);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, 'https://api.parse.com/1/files/' . $filename);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

function add() {

    if ($this->request->is('post')) {

        $giftUniqueId = time();
        $urlFileImage = $allData['Coupon']['imageLink']; 
        //Eg : "http://localhost/favrapp/img/couponsLogo/gap.jpg";
        $filename = $allData['Coupon']['imageName']; //Eg : "gap.jpg";

        $responseFile = $this->sendImageToParse($urlFileImage, $filename);
        $arrayInt = json_decode($responseFile);

        if (!empty($arrayInt)) {
            $name = $arrayInt->name;
            $url = $arrayInt->url;
        } else {
            $name = '';
            $url = '';
        }

        //Save to table by creating object   
        $url = 'https://api.parse.com/1/classes/giftcard';
        $data = array('name' => $allData['Coupon']['name'], 
               'couponType' => $allData['Coupon']['coupontype'],
               'giftcardTypeid' => $allData['Coupon']['giftcardTypeid'], 'giftcardcheckid' => $giftUniqueId,
            'logo' => array('name' => $name, '__type' => 'File', 'url' => $url),
        );
        $_data = json_encode($data);
        $headers = array(
            'X-Parse-Application-Id: ' . $this->APPLICATION_ID,
            'X-Parse-REST-API-Key: ' . $this->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);
        curl_exec($curl);
        curl_close($curl);
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.