用curl来提交数据但是没有效果想问一下 需要如何实现这个功能asp中表单:<form name="form1" method="post" action="index-cs.aspx" id="form1" enctype="multipart/form-data"></form>
是否需要伪造个头信息??

解决方案 »

  1.   

    1、你只给了一个空表单。谁知道需要提交什么?
    2、表单有 enctype 属性,应该是有文件上传的
    3、你没有给出提交的 php 代码,让人如何帮助你
      

  2.   

    stream_context_create的方式:<?php 
    function do_post_request($url, $postdata, $files = null) 

        $data = ""; 
        $boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10); 
           
        //Collect Postdata 
        foreach($postdata as $key => $val) 
        { 
            $data .= "--$boundary\n"; 
            $data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n"; 
        } 
         
        $data .= "--$boundary\n"; 
        
        //Collect Filedata 
        foreach($files as $key => $file) 
        { 
            $fileContents = file_get_contents($file['tmp_name']); 
            
            $data .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$file['name']}\"\n"; 
            $data .= "Content-Type: image/jpeg\n"; 
            $data .= "Content-Transfer-Encoding: binary\n\n"; 
            $data .= $fileContents."\n"; 
            $data .= "--$boundary--\n"; 
        } 
      
        $params = array('http' => array( 
               'method' => 'POST', 
               'header' => 'Content-Type: multipart/form-data; boundary='.$boundary, 
               'content' => $data 
            ));    $ctx = stream_context_create($params); 
       $fp = fopen($url, 'rb', false, $ctx); 
       
       if (!$fp) { 
          throw new Exception("Problem with $url, $php_errormsg"); 
       } 
      
       $response = @stream_get_contents($fp); 
       if ($response === false) { 
          throw new Exception("Problem reading data from $url, $php_errormsg"); 
       } 
       return $response; 
    } //set data (in this example from post) //sample data 
    $postdata = array( 
        'name' => $_POST['name'], 
        'age' => $_POST['age'], 
        'sex' => $_POST['sex'] 
    ); //sample image 
    $files['image'] = $_FILES['image']; do_post_request("http://example.com", $postdata, $files); 
    ?>或者通过CURL的这个参数:
    CURLOPT_POSTFIELDS  全部数据使用HTTP协议中的"POST"操作来发送。要发送文件,在文件名前面加上@前缀并使用完整路径。这个参数可以通过urlencoded后的字符串类似'para1=val1&para2=val2&...'或使用一个以字段名为键值,字段数据为值的数组。如果value是一个数组,Content-Type头将会被设置成multipart/form-data。
      

  3.   

    [[email protected] htdocs]$ php curl.php 
    Array
    (
        [file] => Array
            (
                [name] => log.txt
                [type] => application/octet-stream
                [tmp_name] => /tmp/phpSak15H
                [error] => 0
                [size] => 4
            ))
    Array
    (
        [sub] => this is submit
    )
    [[email protected] htdocs]$ cat curl.php 
    <?php
    $curl = curl_init('http://localhost:8500/upload.php');
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, array('sub' => 'this is submit', 'file' => '@/home/liangdong/libs/htdocs/log.txt'));
    curl_exec($curl);
    ?>
    [[email protected] htdocs]$ cat upload.html 
    <html>
    <head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>
    <body>
    <form method="post" action="upload.php" enctype="multipart/form-data">
            <input type="file" name="upload" />
            <input type="submit" name="sub" />
    </form>
    </body>
    </html>