2012-07-07 80 views
2

目前使用这个,但存储的文件是空的,所以我supose没有数据正在经历。如何将图片上传到parse.com usint REST API + PHP?

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

回答

6

得到它的工作。您需要将二进制数据作为后置字段发送。咄。 在此之前,你可能需要创建一些限制(文件大小,类型等)

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

使用本作品完全一样,我们需要.....

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

}