php模拟post提交文件图片等

Posted 西瓜霜^_^

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php模拟post提交文件图片等相关的知识,希望对你有一定的参考价值。

<?php
    /**
     * Email [email protected]
     * author jackluo
     * 2014.11.21
     * 
     */

    //*
    function curl_post($url, $data, $header = array()){
            if(function_exists(‘curl_init‘)) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                if(is_array($header) && !empty($header)){
                    $set_head = array();
                    foreach ($header as $k=>$v){
                        $set_head[] = "$k:$v";
                    }
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $set_head);
                }
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 1);// 1s to timeout.
                $response = curl_exec($ch);
                if(curl_errno($ch)){
                    //error
                    return curl_error($ch);
                }
                $reslut = curl_getinfo($ch);
                print_r($reslut);
                curl_close($ch);
                $info = array();
                if($response){
                    $info = json_decode($response, true);
                }
                return $info;
            } else {
                throw new Exception(‘Do not support CURL function.‘);
            }
    }
    //*/
    //  
    function api_notice_increment($url, $data)
    {
        $ch = curl_init();        
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
//        $data = http_build_query($data);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        //curl_file_create
    //    $result =  curl_exec($ch);
        $lst[‘rst‘] = curl_exec($ch);
        $lst[‘info‘] = curl_getinfo($ch);
        curl_close($ch); 
    
        return $lst;
    //    return $result;
    }

     /**
         *  curl文件上传
         *  @var  struing  $r_file  上传文件的路劲和文件名  
         *     
         */
    /*     
    function upload_file($url,$r_file)
     {
        $file = array("fax_file"=>‘@‘.$r_file,‘type‘=>‘image/jpeg‘);//文件路径,前面要加@,表明是文件上传.
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_POSTFIELDS,$file);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        $result = curl_exec($curl);  //$result 获取页面信息 
        curl_close($curl);
        echo $result ; //输出 页面结果
   }*/
    
   function upload_file($url,$filename,$path,$type){
        $data = array(
            ‘pic‘=>‘@‘.realpath($path).";type=".$type.";filename=".$filename
        );
        $ch = curl_init();

   //设置帐号和帐号名

   curl_setopt($ch, CURLOPT_USERPWD, ‘joe:secret‘ );

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // curl_getinfo($ch);
        $return_data = curl_exec($ch);
        curl_close($ch);
        echo $return_data;       
   }

  // php 5.5 以后请用以下函数

function upload_file($url,$filename,$path,$type){
          $data = array(
                ‘pic‘=>new CURLFile(realpath($path))
          );
          $ch = curl_init();
   //也可以用以下注释掉的不用改代码,觉得新版的可以省下点代码,看个人
   //curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
   //设置帐号和帐号名
   curl_setopt($ch, CURLOPT_USERPWD, ‘joe:secret‘ );
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_POST, true );
          curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
          curl_setopt($ch, CURLOPT_HEADER, false);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          // curl_getinfo($ch);
          $return_data = curl_exec($ch);
          curl_close($ch);
          echo $return_data;       
   }




    if ($_POST) {
        $url = ‘http://platform.com/upload/image‘;
        //
        $path = $_SERVER[‘DOCUMENT_ROOT‘];
/*
        print_r($_FILES);
        exit;
*/
        //$filename = $path."/232.jpg";
        //upload tmp
        $tmpname = $_FILES[‘fname‘][‘name‘];
        $tmpfile = $_FILES[‘fname‘][‘tmp_name‘];
        $tmpType = $_FILES[‘fname‘][‘type‘];
//        echo $tmpType;
        upload_file($url,$tmpname,$tmpfile,$tmpType);
        /*
        $data = array(
                ‘path‘=>"@$path/232.jpg",
                ‘name‘=>‘h‘
        );
        */
        //‘pic‘=>‘@/tmp/tmp.jpg‘, ‘filename‘=>‘tmp‘
        //$data = array(‘pic‘=>"@$filename", ‘filename‘=>‘tmp‘);
/*
        $data = array(
            ‘uid‘    =>    10086,
            ‘pic‘    =>    ‘@$tmpfile‘.‘;type=‘.$tmpType
        );
        $info = api_notice_increment($url, $data);
*/
        //$info = curl_post($url, $data);
        //$info = api_notice_increment($url, $data);
        //upload_file($url,$tmpfile);
        //print_r($info);
        exit;
/*
        $file = ‘H:\www\test\psuCARGLSPA-pola.jpg‘; //要上传的文件
        $src = upload_curl_pic($file);
        echo $src;
*/
    }    
?>

<form action="http://localhost/upload.php" enctype="multipart/form-data"  method="post">
  <p>UpLoad: <input type="text" name="fname" /></p>
  <p>UpLoad: <input type="file" name="fname" /></p>

  <input type="submit" value="Submit" />
</form>

 

以上是关于php模拟post提交文件图片等的主要内容,如果未能解决你的问题,请参考以下文章

PHP cURL抓取网上图片

CURL模拟POST请求实例

请教用Curl 在php 里面模拟表单提交 文本+文件的写法

PHP模拟POST提交数据并获得返回值之CURL方法(使用PHP extension,然后使用php_curl.dll,很不错)

C# 模拟 post提交php页面的表单

PHP 中的CURL 模拟表单的post提交