Home  >  Article  >  Backend Development  >  200分CURL设置HTTPHEADER上传文件有关问题!(补充提问)

200分CURL设置HTTPHEADER上传文件有关问题!(补充提问)

WBOY
WBOYOriginal
2016-06-13 13:35:50893browse

200分求助CURL设置HTTPHEADER上传文件问题!(补充提问)

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$boundary   = md5(time());
$postStr  = "";
$postStr .="--".$boundary."\r\n";
$postStr .="Content-Disposition: form-data; name=\"uptxt[]\"; filename=\"index_1.html\"\r\n";
$postStr .="Content-Type: text/html\r\n\r\n";
$postStr .=$uploadFile."\r\n"; #这里是部分文件内容
$postStr .="--".$boundary."\r\n";



$postStr .="Content-Disposition: form-data; name=\"uptxt[]\"; filename=\"index_1.html\"\r\n";
$postStr .="Content-Type: text/html\r\n\r\n";

改为

$postStr .="Content-Disposition: form-data; name=\"uptxt[]\"; filename=\"index_1.html\""; //删除\r\n
$postStr .="Content-Type: text/html"; //删除\r\n

之后还是可以上传成功 


而且

$postStr .="Content-Disposition: form-data; name=\"uptxt[]\"; filename=\"index_1.html\"\r\n";
$postStr .="Content-Type: text/html\r\n\r\n";

改为

$postStr .='Content-Disposition: form-data; name="uptxt[]"; filename="index_1.html"\r\n';//改为单引号,\r\n不再转义
$postStr .='Content-Type: text/html\r\n\r\n'; //改为单引号,\r\n不再转义

也可以上传成功,求解

------解决方案--------------------
如你所改,我测试,接收端,没有获取到期望的信息

或贴完整代码看看?
------解决方案--------------------
协议就是要来遵守的,如果不遵守,后果的不可预知的。
你能请求成功,但是会让web服务器解析http时乱套,/号和换行符都是重要的符号标志。

比方说现在boundary块如下,注意Content-type:text~html,去除\r\n且把/换成~
PHP code
$boundary   = md5(time());
$postStr  = ""; 
$postStr .="--".$boundary."\r\n";
$postStr .="Content-Disposition: form-data; name=\"uptxt[]\"; filename=\"index_1.html\"";
$postStr .="Content-Type: text~html";                                                                                                       
$postStr .="123\r\n"; #这里是部分文件内容
$postStr .="--".$boundary."\r\n";
<br><font color="#e78608">------解决方案--------------------</font><br>不是用curl吗?网上找了一段是这样的:<br>
Perl code

<?php $url  = 'http://www.myheritage.cn/FP/Company/tryFaceRecognition.php';//target url
 
$fields['file'] = '@'.'C:\Users\lzyy\Desktop\3431821560_5e18c02221.jpg';
$fields['lang'] = 'ZH';
$fields['morph'] = '1';
$fields['popup'] = 'PJSLVMMT';
$fields['loadMethod'] = 'myFiles';
 
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, "http://www.myheritage.cn/");
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);//get redirect content
 
curl_exec( $ch );
//$rs = curl_exec($ch);
 
if ($error = curl_error($ch) ) {
          die($error);
}
curl_close($ch);
//print_r(htmlspecialchars($rs));
echo 'ok';
?>
<br><font color="#e78608">------解决方案--------------------</font><br>
探讨
协议就是要来遵守的,如果不遵守,后果的不可预知的。
你能请求成功,但是会让web服务器解析http时乱套,/号和换行符都是重要的符号标志。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn