Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427

    invio tramite Curl e parametr

    function doPut($url,$fields,$file)
    {
    $fields = (is_array($fields)) ? http_build_query($fields) : $fields;

    if($ch = curl_init($url))
    {
    $fp = fopen($file, "r");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_PUT, true);
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($fields)));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);

    print_r (curl_exec($ch));

    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    $t = curl_close($ch);

    return $t;
    }
    else
    {
    return false;
    }
    }

    Usando questa funzione dovrei in teoria riuscire ad inviare un file e a passare i parametri richiesti come argomenti "POST"

    Il problema è che il webserver non riconosce il primo parametro nell'array fields...

    che è

    $fields = array(
    'action' => 'document.upload',
    'apiKey' => $apikey,
    'name' => 'racing',
    'title'=>'Race Cars',
    'signature'=>$str);

    a mio avviso potrebbe essere un errore dovuto al fatot che il webservice si aspetta uan codifica di invio con enctype

    <form action="mio_server.php" enctype="multipart/form-data" method="post">

    la domanda quindi è:
    come posso inviare un file tramite curl e passare i parametri con metodo multipart/form-data ?
    :master:
    2000 post e sono più vecchio di 4 anni...
    grazie a tutti....

  2. #2
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427
    ho trovato questo nel manuale..

    CURLOPT_POSTFIELDS The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

    e ho fatto come richiesto:

    $fields = array('action' => 'document.upload',
    'apiKey' => $apikey,
    'name' => 'racing',
    'title'=>'Race Cars',
    'signature'=>$str);

    function doPut($url,$fields,$file)
    {
    $fields = (is_array($fields)) ? http_build_query($fields) : $fields;

    if($ch = curl_init($url))
    {
    $fp = fopen(@$file, "r");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_PUT, true);
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
    curl_setopt($ch, CURLOPT_UPLOAD, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($fields),'Content-Type:multipart/form-data'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    print(curl_exec($ch));

    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    $t = curl_close($ch);

    }
    else
    {
    return false;
    }
    }


    ma il webservice non trova il campo "action"
    2000 post e sono più vecchio di 4 anni...
    grazie a tutti....

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.