$file = 'file'; //要上传的文件 $url = 'url';//target url $fields['f'] = '@'.$file; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url ); curl_setopt($ch, CURLOPT_POST, 1 ); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields ); curl_exec( $ch ); if ($error = curl_error($ch) ) { die($error); } curl_close($ch);
这里说明一下CURLOPT_POSTFIELDS这个参数如果是POST字符串时,可以用形如“name=value&...”的字符串,如果post文件,就必须要用数组,并且文件名格式为"@绝对路径",这是 CURL 会帮你做 multipart/form-data编码。
我应该找个CURL的手册看,比用google快多了,欲速则不达呀。
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_VERBOSE, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)"); curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL); curl_setopt($ch, CURLOPT_POST, true); // same as <input type="file" name="file_box"> $post = array( "file_box"=>"@/path/to/myfile.jpg", ); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $response = curl_exec($ch); ?>