Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maitret/2877b8b6ad73f46d906aa4055c4ae621 to your computer and use it in GitHub Desktop.
Save maitret/2877b8b6ad73f46d906aa4055c4ae621 to your computer and use it in GitHub Desktop.

Revisions

  1. @ramingar ramingar revised this gist Sep 16, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions php-curl-request-multipart.md
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,8 @@ $headers[] = "Authorization: Bearer " . $apiToken;

    $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);
    //$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,
  2. @ramingar ramingar revised this gist Sep 16, 2016. No changes.
  3. @ramingar ramingar revised this gist Sep 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-curl-request-multipart.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    $ch = curl_init();

    $headers = array();
    $headers[] = "Token: " . $apiToken;
    $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);
  4. @ramingar ramingar revised this gist Sep 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-curl-request-multipart.md
    Original 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, el boundary y el base64 se genera mal.
    //$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);

  5. @ramingar ramingar revised this gist Sep 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-curl-request-multipart.md
    Original 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. Usa solo cuando es un x-www-form-urlencoded.
    // 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
    ]
  6. @ramingar ramingar revised this gist Sep 13, 2016. 1 changed file with 9 additions and 11 deletions.
    20 changes: 9 additions & 11 deletions php-curl-request-multipart.md
    Original 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 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.
    //$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 usen '@'. Genera errores en el base64. Desde PHP 5.5 está obsoleto. En su lugar usar CURLFile.
    //$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_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => $post,

    // CURLOPT_POST => true,
    // CURLOPT_PROXY => 'localhost:8080',
    // CURLOPT_SSL_VERIFYPEER => false,
    // 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);
    $response = curl_getinfo($ch);
    $aa = curl_multi_getcontent($ch);
    curl_close($ch);
    $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

    return null;
    curl_close($ch);
    ```
  7. @ramingar ramingar revised this gist Sep 13, 2016. No changes.
  8. @ramingar ramingar revised this gist Sep 13, 2016. 1 changed file with 13 additions and 12 deletions.
    25 changes: 13 additions & 12 deletions php-curl-request-multipart.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    ```php
    <?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($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);
    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);
  9. @ramingar ramingar revised this gist Sep 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-curl-request-multipart.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ``` php
    ```php
    <?php

    $ch = curl_init();
  10. @ramingar ramingar revised this gist Sep 13, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions php-curl-request-multipart.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    ``` php
    <?php

    $ch = curl_init();

    $headers = array();
  11. @ramingar ramingar created this gist Sep 13, 2016.
    30 changes: 30 additions & 0 deletions php-curl-request-multipart.md
    Original 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;
    ```