php>=5.5版本,使用PHP CURL POST文件上传

参考:http://blog.csdn.net/ljfrocky/article/details/46009251

php55CurlPost.php

<?php
 
define('FORM_BOUNDARY', uniqid('----WebKitFormBoundary'));
define('FORM_HYPHENS', '--');
define('FORM_EOL', "\r\n");
define('FORM_IMG_FIELD_NAME', 'img[]');
 
/*
 * 要POST的文本数据和本地图片
 */
$text_data = [
    'username' => 'Tom',
    'password' => '123456',
];
$img_data = [
    '111.jpg' => '/Applications/XAMPP/xamppfiles/htdocs/indomeme.yyy/111.jpg',
    '222.jpg' => '/Applications/XAMPP/xamppfiles/htdocs/indomeme.yyy/222.jpg',
];
 
$url = 'http://indomeme.yyy/php55Upload.php'; // 服务器URL,根据实际情况进行修改
$req_headers = [
    'Content-Type: multipart/form-data; boundary=' . FORM_BOUNDARY,
];
$post_data = '';
 
array_walk($text_data, 'text_form_data_splice');
array_walk($img_data, 'image_form_data_splice');
if (!empty($post_data)) {
    $post_data .= FORM_HYPHENS . FORM_BOUNDARY . FORM_HYPHENS;
}
 
$result = curl_post($url, $req_headers, $post_data);
 
var_dump($result);
 
/**
 * 拼接文本类型的表单项
 * @param string $value 表单项的域值
 * @param string $key 表单项的域名
 */
function text_form_data_splice($value, $key) {
    global $post_data;
    $post_data .= FORM_HYPHENS . FORM_BOUNDARY . FORM_EOL
               . "Content-Disposition: form-data; name=\"$key\""
               . FORM_EOL . FORM_EOL
               . $value . FORM_EOL;
}
 
/**
 * 拼接上传图片类型的表单项
 * @param string $value 图片在本地的路径
 * @param string $key 图片名称
 */
function image_form_data_splice($value, $key) {
    global $post_data;
 
    $image_type = exif_imagetype($value);
    if ($image_type === false) {
        return;
    }
    $mime_type = image_type_to_mime_type($image_type);
    $post_data .= FORM_HYPHENS . FORM_BOUNDARY . FORM_EOL
               . "Content-Disposition: form-data; name=\"" . FORM_IMG_FIELD_NAME
               . "\"; filename=\"$key\"" . FORM_EOL
               . "Content-Type: $mime_type" . FORM_EOL . FORM_EOL
               . file_get_contents($value) . FORM_EOL;
}
 
function curl_post($url, array $header, $data) {
    $options = [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER => false,
        CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_POSTFIELDS => $data,
    ];
 
    $ch = curl_init();
    curl_setopt_array($ch, $options);
    $resp = curl_exec($ch);
    if (curl_errno($ch)) {
        $ret = [
            'code' => -1,
            'resp' => curl_error($ch),
        ];
    } else {
        $ret = [
            'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
            'resp' => $resp,
        ];
    }
    curl_close($ch);
 
    return $ret;
}

php55Upload.php

<?php
var_dump($_FILES);
var_dump($_POST);

使用curl模拟post多数据到远程服务器

由于使用get方式传输的数据量太少,故而使用post,注意设置超时时间
更多POST方式可以参考http://www.oschina.net/code/snippet_127872_6370

function request_by_curl($remote_server,$post_string,$second=60){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$remote_server);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$post_string);
    curl_setopt($ch,CURLOPT_TIMEOUT,$second); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2');
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

现在系统代码中经常会使用到file_get_contents这个函数同样也可以post,注意设置超时时间
参考:http://www.cnblogs.com/samson/archive/2011/08/12/2135834.html

<?php
function Post($url, $post = null,$type='POST')
{
     $context = array();
     if (is_array($post))
     {
         $opts = array(
            'http' => array(
              'timeout'=>60,
                'method'=>$type,
              'content' => http_build_query($post),
                'header' => 'Content-Type:application/x-www-form-urlencoded;',
            ),
        );
  if($type!='POST')
  {
   $url .= '?'.$opts['http']['content'];
  }
     }
     return file_get_contents($url, false, stream_context_create($opts));
}
$data = array
(
     'name' => 'test',
     'email' => '[email protected]',
     'submit' => 'submit',
);
$cnt=0;
while($cnt < 3 && ($str=Post('http://localhost/test5.php', $data,'POST'))===FALSE)
{
 $cnt++;
}
var_dump($str);
?>