将$ FILE发送到远程URL并获取XML响应

时间:2011-07-13 13:11:05

标签: php forms file

我想模拟以下表单并获取xml响应:

<form action="https://s7ugc3.scene7.com/ugc/image?op=upload&upload_token=<?php //echo CDN::getS7Token(); ?>&company_name=usineadesign" method="post" enctype="multipart/form-data">
        <p>
                Formulaire d'envoi de fichier :<br />
                <input type="file" name="image" /><br />
                <input type="submit" value="Envoyer le fichier" />
        </p>
</form>

图片在服务器上,我可以轻松访问其路径!我想创建一个看起来像

的函数
uploadtoscen7($path_to_image)
{
  ...
  return $url;
}

感谢任何可以帮助我的人!

1 个答案:

答案 0 :(得分:0)

我建议您使用cURL发布到远程HTTP服务器。您需要相应地设置POSTDATA。我使用此函数从HTTP服务器发送/获取数据:

function get_page_by_curl($searchUrl, $post=false, $postParams="")
{
    print " " . $searchUrl;
    global $errMsg;
    //$userAgent = "Googlebot/2.1( http://www.googlebot.com/bot.html)"; 
    $userAgent = "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.5 Robot";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_URL, $searchUrl);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_NOPROGRESS, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    if($post)
    {
         curl_setopt ($ch, CURLOPT_POST, true);
         curl_setopt ($ch, CURLOPT_POSTFIELDS, $postParams);
    }
    $htmlPage = false;
    do
    {
        $htmlPage = curl_exec($ch);
        $errno = curl_errno($ch);
        if($errno == 28)
        {
            print ".";
            flush();
            sleep(SLEEP_TIME);
        }
        elseif($errno == 7)
        {
            print "*";
            flush();
            sleep(SLEEP_TIME);
        }
        elseif($errno == 6)
        {
            print "+";
            flush();
            sleep(SLEEP_TIME);
        }
        elseif($errno != 0)
        {
            $errMsg = $errno . ": " . curl_error($ch);
        }
    }
    while(!$htmlPage && $errno == 28);

    return $htmlPage;
}

你应该可以这样称呼它:

$xml = get_page_by_curl($url, true, 'image=@/full/path/to/file&submit=Envoyer+le+fichier');