par
Testa » 09 sept. 2013, 16:29
C'est la première fois que je demande de l'aide en php mais je commence à vraiment devenir fatigué alors je suppose que j'ai pas le choix parce que je suis vraiment bloqué ><.
J'essaie depuis ce matin d'uploader des fichiers via l'api de filecloud.io (
http://code.google.com/p/filecloud/), j'ai d'abord testé leur api sur mon instance Gandi (info php ->
http://pastebin.com/S4Dx3RyP), elle marchait très bien et j'ai pu faire ce que je voulais sauf que dés que je suis passé sur une autre configuration, celle d'olympe (
http://pastebin.com/7FfsY3Ky) l'api renvoyais "no file uploaded [err #1009]"
Voila la fonction d'upload de l'api en question :
public function upload( $filepath, $apikey ){
//check the file is actually readable
if ( (is_readable($filepath) == false ) || (is_file( $filepath ) == false )){
throw new FilecloudApiException('Input file is not readable');
}
//first things first lets determine an upload server
try {
$response = $this->post(
'http://api.filecloud.io/api-fetch_upload_url.api',
array()
);
if ( array_key_exists('upload_url', $response) == true ){
$uploadUrl = $response['upload_url'];
}
else{
throw new FilecloudApiException('No upload url available, uploads disabled?');
}
}
catch ( FilecloudApiException $e){
throw new FilecloudApiException( $e->getMessage() );
}
//the users apikey
$params['akey'] = $apikey;
//add our file to POST field
$params["Filedata"] = "@".$filepath;
//secondly lets upload the file via php curl
if ( ( $handle = curl_init() ) === false ) {
throw new FilecloudApiException('Unable to create curl handle, check php curl extension is available?');
}
//set request options
curl_setopt( $handle, CURLOPT_URL, $uploadUrl );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 5 );
curl_setopt( $handle, CURLOPT_POST, true );
curl_setopt( $handle, CURLOPT_POSTFIELDS, $params );
//execute the request
if ( ($result = curl_exec( $handle )) === false) {
$code = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
throw new FilecloudApiException('HTTP upload failed. HTTPCODE #'.$code );
}
//check for http 200 ok code
$code = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
if ($code != '200') {
throw new FilecloudApiException('HTTP upload failed. HTTPCODE #'.$code );
}
//close the connection
curl_close($handle);
//decode our response
$result = json_decode( $result,true );
//check the response is correctly formatted
if (
( is_array( $result ) === true )
&&
( array_key_exists('status', $result ) == true )
&&
( $result['status'] == 'ok')
){
//add url to result
$result['url'] = 'http://filecloud.io/'.$result['ukey'];
return $result;
}
else{
if ( ( is_array( $result ) === true ) &&( array_key_exists('message', $result ) == true )){
throw new FilecloudApiException( $result['message'] );
}
else{
throw new FilecloudApiException( 'Badly formatted response received' );
}
}
}
J'avais déjà eu des problèmes liés aux requêtes cUrl sur la plateforme, pour les pallier j'utilisais file_get_content donc c'est ce que j'ai fait ici aussi :
public function upload( $filepath, $apikey ){
//check the file is actually readable
if ( (is_readable($filepath) == false ) || (is_file( $filepath ) == false )){
throw new FilecloudApiException('Input file is not readable');
}
//first things first lets determine an upload server
try {
$response = $this->post(
'http://api.filecloud.io/api-fetch_upload_url.api',
array()
);
if ( array_key_exists('upload_url', $response) == true ){
$uploadUrl = $response['upload_url'];
}
else{
throw new FilecloudApiException('No upload url available, uploads disabled?');
}
}
catch ( FilecloudApiException $e){
throw new FilecloudApiException( $e->getMessage() );
}
define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));
$header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;
define('FORM_FIELD', 'Filedata');
$file_contents = file_get_contents($filepath);
$content = "--".MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"".FORM_FIELD."\"; filename=\"".basename($filepath)."\"\r\n".
"Content-Type: application/x-rar-compressed\r\n\r\n".
$file_contents."\r\n";
// add some POST fields to the request too: $_POST['foo'] = 'bar'
$content .= "--".MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"akey\"\r\n\r\n".
"$apikey\r\n";
// signal end of request (note the trailing "--")
$content .= "--".MULTIPART_BOUNDARY."--\r\n";
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $header,
'content' => $content,
),
));
$result = file_get_contents($uploadUrl, false, $context, -1, 50000);
//decode our response
$result = json_decode( $result,true );
//check the response is correctly formatted
if (
( is_array( $result ) === true )
&&
( array_key_exists('status', $result ) == true )
&&
( $result['status'] == 'ok')
){
//add url to result
$result['url'] = 'http://filecloud.io/'.$result['ukey'];
return $result;
}
else{
if ( ( is_array( $result ) === true ) &&( array_key_exists('message', $result ) == true )){
throw new FilecloudApiException( $result['message'] );
}
else{
throw new FilecloudApiException( 'Badly formatted response received' );
}
}
}
Mais même résultat : ça marche chez gandi et pas sur mon instance olympe ><.
Code : Tout sélectionner
Warning: file_get_contents(): length must be greater than or equal to zero in code overridden on append 49 on line 1
FilecloudApiException > Badly formatted response received
Je comprend vraiment rien à pourquoi ça pourrait marcher dans un cas et pas dans l'autre je suis pas habitué aux requêtes http alors je m'en remet à vous =S. Merci énormément pour votre attention.
C'est la première fois que je demande de l'aide en php mais je commence à vraiment devenir fatigué alors je suppose que j'ai pas le choix parce que je suis vraiment bloqué ><.
J'essaie depuis ce matin d'uploader des fichiers via l'api de filecloud.io (http://code.google.com/p/filecloud/), j'ai d'abord testé leur api sur mon instance Gandi (info php -> http://pastebin.com/S4Dx3RyP), elle marchait très bien et j'ai pu faire ce que je voulais sauf que dés que je suis passé sur une autre configuration, celle d'olympe (http://pastebin.com/7FfsY3Ky) l'api renvoyais "no file uploaded [err #1009]"
Voila la fonction d'upload de l'api en question :
[php]public function upload( $filepath, $apikey ){
//check the file is actually readable
if ( (is_readable($filepath) == false ) || (is_file( $filepath ) == false )){
throw new FilecloudApiException('Input file is not readable');
}
//first things first lets determine an upload server
try {
$response = $this->post(
'http://api.filecloud.io/api-fetch_upload_url.api',
array()
);
if ( array_key_exists('upload_url', $response) == true ){
$uploadUrl = $response['upload_url'];
}
else{
throw new FilecloudApiException('No upload url available, uploads disabled?');
}
}
catch ( FilecloudApiException $e){
throw new FilecloudApiException( $e->getMessage() );
}
//the users apikey
$params['akey'] = $apikey;
//add our file to POST field
$params["Filedata"] = "@".$filepath;
//secondly lets upload the file via php curl
if ( ( $handle = curl_init() ) === false ) {
throw new FilecloudApiException('Unable to create curl handle, check php curl extension is available?');
}
//set request options
curl_setopt( $handle, CURLOPT_URL, $uploadUrl );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 5 );
curl_setopt( $handle, CURLOPT_POST, true );
curl_setopt( $handle, CURLOPT_POSTFIELDS, $params );
//execute the request
if ( ($result = curl_exec( $handle )) === false) {
$code = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
throw new FilecloudApiException('HTTP upload failed. HTTPCODE #'.$code );
}
//check for http 200 ok code
$code = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
if ($code != '200') {
throw new FilecloudApiException('HTTP upload failed. HTTPCODE #'.$code );
}
//close the connection
curl_close($handle);
//decode our response
$result = json_decode( $result,true );
//check the response is correctly formatted
if (
( is_array( $result ) === true )
&&
( array_key_exists('status', $result ) == true )
&&
( $result['status'] == 'ok')
){
//add url to result
$result['url'] = 'http://filecloud.io/'.$result['ukey'];
return $result;
}
else{
if ( ( is_array( $result ) === true ) &&( array_key_exists('message', $result ) == true )){
throw new FilecloudApiException( $result['message'] );
}
else{
throw new FilecloudApiException( 'Badly formatted response received' );
}
}
}[/php]
J'avais déjà eu des problèmes liés aux requêtes cUrl sur la plateforme, pour les pallier j'utilisais file_get_content donc c'est ce que j'ai fait ici aussi :
[php]public function upload( $filepath, $apikey ){
//check the file is actually readable
if ( (is_readable($filepath) == false ) || (is_file( $filepath ) == false )){
throw new FilecloudApiException('Input file is not readable');
}
//first things first lets determine an upload server
try {
$response = $this->post(
'http://api.filecloud.io/api-fetch_upload_url.api',
array()
);
if ( array_key_exists('upload_url', $response) == true ){
$uploadUrl = $response['upload_url'];
}
else{
throw new FilecloudApiException('No upload url available, uploads disabled?');
}
}
catch ( FilecloudApiException $e){
throw new FilecloudApiException( $e->getMessage() );
}
define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));
$header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;
define('FORM_FIELD', 'Filedata');
$file_contents = file_get_contents($filepath);
$content = "--".MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"".FORM_FIELD."\"; filename=\"".basename($filepath)."\"\r\n".
"Content-Type: application/x-rar-compressed\r\n\r\n".
$file_contents."\r\n";
// add some POST fields to the request too: $_POST['foo'] = 'bar'
$content .= "--".MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"akey\"\r\n\r\n".
"$apikey\r\n";
// signal end of request (note the trailing "--")
$content .= "--".MULTIPART_BOUNDARY."--\r\n";
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $header,
'content' => $content,
),
));
$result = file_get_contents($uploadUrl, false, $context, -1, 50000);
//decode our response
$result = json_decode( $result,true );
//check the response is correctly formatted
if (
( is_array( $result ) === true )
&&
( array_key_exists('status', $result ) == true )
&&
( $result['status'] == 'ok')
){
//add url to result
$result['url'] = 'http://filecloud.io/'.$result['ukey'];
return $result;
}
else{
if ( ( is_array( $result ) === true ) &&( array_key_exists('message', $result ) == true )){
throw new FilecloudApiException( $result['message'] );
}
else{
throw new FilecloudApiException( 'Badly formatted response received' );
}
}
}[/php]
Mais même résultat : ça marche chez gandi et pas sur mon instance olympe ><.
[code]Warning: file_get_contents(): length must be greater than or equal to zero in code overridden on append 49 on line 1
FilecloudApiException > Badly formatted response received[/code]
Je comprend vraiment rien à pourquoi ça pourrait marcher dans un cas et pas dans l'autre je suis pas habitué aux requêtes http alors je m'en remet à vous =S. Merci énormément pour votre attention.