コード例 #1
0
 private function upload()
 {
     $fileParts = explode('.', $this->getName());
     $extension = array_pop($fileParts);
     $mimeType = $this->mimeType ?: $this->getMimeTypeForExtension($extension);
     $headers = ParseClient::_getRequestHeaders(null, false);
     $url = ParseClient::getAPIUrl() . 'files/' . $this->getName();
     $rest = curl_init();
     curl_setopt($rest, CURLOPT_URL, $url);
     curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($rest, CURLOPT_BINARYTRANSFER, 1);
     $headers[] = 'Content-Type: ' . $mimeType;
     curl_setopt($rest, CURLOPT_POST, 1);
     curl_setopt($rest, CURLOPT_POSTFIELDS, $this->getData());
     curl_setopt($rest, CURLOPT_HTTPHEADER, $headers);
     $response = curl_exec($rest);
     $contentType = curl_getinfo($rest, CURLINFO_CONTENT_TYPE);
     if (curl_errno($rest)) {
         throw new ParseException(curl_error($rest), curl_errno($rest));
     }
     curl_close($rest);
     if (strpos($contentType, 'text/html') !== false) {
         throw new ParseException('Bad Request', -1);
     }
     $decoded = json_decode($response, true);
     if (isset($decoded['error'])) {
         throw new ParseException($decoded['error'], isset($decoded['code']) ? $decoded['code'] : 0);
     }
     return $decoded;
 }