Forked from ramingar/php-curl-request-multipart.md
          
        
    
          Created
          September 15, 2021 17:01 
        
      - 
      
- 
        Save maitret/2877b8b6ad73f46d906aa4055c4ae621 to your computer and use it in GitHub Desktop. 
Revisions
- 
        ramingar revised this gist Sep 16, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,8 +7,8 @@ $headers[] = "Authorization: Bearer " . $apiToken; $cfile = new \CURLFile($fileNameWithFullPath); //$post = array($fieldName => '@/tmp/phpDT8j'); // ERROR: no uses '@'. Genera errores en el base64. Desde PHP 5.5 está obsoleto. En su lugar usar CURLFile. $post = array($fieldName => $cfile); // e.g. $fieldName = 'image' curl_setopt_array( $ch, 
- 
        ramingar revised this gist Sep 16, 2016 . No changes.There are no files selected for viewing
- 
        ramingar revised this gist Sep 16, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ $ch = curl_init(); $headers = array(); $headers[] = "Authorization: Bearer " . $apiToken; //$headers[] = "Content-Type: multipart/form-data"; // ERROR: no uses esta cabecera. Si $post es un array, la cabecera se añade automáticamente. Si se añade manualmente, me ha pasado que el boundary o el base64 se genera mal. $cfile = new \CURLFile($fileNameWithFullPath); 
- 
        ramingar revised this gist Sep 16, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ $ch = curl_init(); $headers = array(); $headers[] = "Token: " . $apiToken; //$headers[] = "Content-Type: multipart/form-data"; // ERROR: no uses esta cabecera. Si $post es un array, la cabecera se añade automáticamente. Si se añade manualmente, me ha pasado que el boundary o el base64 se genera mal. $cfile = new \CURLFile($fileNameWithFullPath); 
- 
        ramingar revised this gist Sep 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,7 @@ curl_setopt_array( CURLOPT_URL => $url, CURLOPT_POSTFIELDS => $post, // CURLOPT_POST => true, // ERROR: no uses esta opción. El método se asigna a POST automáticamente cuando usas CURLOPT_POSTFIELDS. Si se usa y el Content-Type es multipart/form-data, el archivo no se sube y se envía un payload vacío. Úsalo solo cuando es un x-www-form-urlencoded. // CURLOPT_PROXY => 'localhost:8080', // Úsalo para depurar! Usar un proxy como Burp Suite te ayuda muchísimo para saber qué request le llega al servidor (en caso de que te sea imposible depurarlo) // CURLOPT_SSL_VERIFYPEER => false, // Esta opción, también para el proxy, se usa para ignorar los certificados en una conexión https que pasa por un proxy ] 
- 
        ramingar revised this gist Sep 13, 2016 . 1 changed file with 9 additions and 11 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,31 +3,29 @@ $ch = curl_init(); $headers = array(); $headers[] = "Token: " . $apiToken; //$headers[] = "Content-Type: multipart/form-data"; // ERROR: no uses esta cabecera. Si $post es un array, la cabecera se añade automáticamente. Si se añade manualmente, el boundary y el base64 se genera mal. $cfile = new \CURLFile($fileNameWithFullPath); //$post = array('image' => '@/tmp/phpDT8j'); // ERROR: no uses '@'. Genera errores en el base64. Desde PHP 5.5 está obsoleto. En su lugar usar CURLFile. $post = array('image' => $cfile); curl_setopt_array( $ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => $headers, CURLOPT_URL => $url, CURLOPT_POSTFIELDS => $post, // CURLOPT_POST => true, // ERROR: no uses esta opción. El método se asigna a POST automáticamente cuando usas CURLOPT_POSTFIELDS. Si se usa y el Content-Type es multipart/form-data, el archivo no se sube y se envía un payload vacío. Usa solo cuando es un x-www-form-urlencoded. // CURLOPT_PROXY => 'localhost:8080', // Úsalo para depurar! Usar un proxy como Burp Suite te ayuda muchísimo para saber qué request le llega al servidor (en caso de que te sea imposible depurarlo) // CURLOPT_SSL_VERIFYPEER => false, // Esta opción, también para el proxy, se usa para ignorar los certificados en una conexión https que pasa por un proxy ] ); $result = curl_exec($ch); // Si el código http de la response es 400+, aquí llega el payload de error de la response $response = curl_getinfo($ch); // Aquí se recogen todas las cabeceras de la response. Solo se recogen las cabeceras si se ha asignado CURLOPT_RETURNTRANSFER a true curl_close($ch); ``` 
- 
        ramingar revised this gist Sep 13, 2016 . No changes.There are no files selected for viewing
- 
        ramingar revised this gist Sep 13, 2016 . 1 changed file with 13 additions and 12 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,4 @@ ```php $ch = curl_init(); $headers = array(); @@ -12,16 +10,19 @@ $cfile = new \CURLFile($fileNameWithFullPath); //$post = array('image' => '@/tmp/phpDT8j'); // ERROR: no usen '@'. Genera errores en el base64. Desde PHP 5.5 está obsoleto. En su lugar usar CURLFile. $post = array('image' => $cfile); curl_setopt_array( $ch, [ CURLOPT_HTTPHEADER => $headers, CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $post, // CURLOPT_POST => true, // CURLOPT_PROXY => 'localhost:8080', // CURLOPT_SSL_VERIFYPEER => false, ] ); $result = curl_exec($ch); $response = curl_getinfo($ch); 
- 
        ramingar revised this gist Sep 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ ```php <?php $ch = curl_init(); 
- 
        ramingar revised this gist Sep 13, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,6 @@ ``` php <?php $ch = curl_init(); $headers = array(); 
- 
        ramingar created this gist Sep 13, 2016 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ ``` php $ch = curl_init(); $headers = array(); $headers[] = "Token: " . $apiToken; //$headers[] = "Content-Type: multipart/form-data"; // ERROR: no usen esta cabecera. Si $post es un array, la cabecera se añade automáticamente. Si se añade manualmente, el boundary y el base64 se genera mal. $cfile = new \CURLFile($fileNameWithFullPath); //$post = array('image' => '@/tmp/phpDT8j'); // ERROR: no usen '@'. Genera errores en el base64. Desde PHP 5.5 está obsoleto. En su lugar usar CURLFile. $post = array('image' => $cfile); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); //curl_setopt($ch, CURLOPT_POST, true); // Send request to Burp Suite (debug) //curl_setopt($ch, CURLOPT_PROXY, 'localhost:8080'); //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); $response = curl_getinfo($ch); $aa = curl_multi_getcontent($ch); curl_close($ch); return null; ```