简体   繁体   English

发送发布请求的多部分/表单数据

[英]Sending a Post request multipart/form-data

I've encountered problem trying to send a Post request and get result, here is my code: 我在尝试发送Post请求并获取结果时遇到问题,这是我的代码:

function connect2($hostname,$method,$path,$data,$cookie){
$fp=fsockopen($hostname, 80, $errno, $errstr, 30);
    if(!$fp) echo "$errstr($errno)<br>/>\n";
    else{
        $headers="$method $path HTTP/1.1\r\n";
        $headers.= "Host: $hostname\r\n";
        if($cookie) $headers.="Cookie: $cookie\r\n";
        if($method=="POST") $headers.="Content-length: ".strlen($data)."\r\n";
        if($method=="POST") $headers.="Content-Type: multipart/form-data;  boundary=---------------------------4358114262792\r\n";
        $headers.="Referer: http://ospb.dev.1adw.com/vt/houses/add\r\n";
        $headers.="Connection: keep-alive\r\n";
        if($method=="POST")$headers.="\r\n\$data\r\n";
        $headers.="\r\n";
        fwrite($fp, $headers);
        $line="";
        while(!feof($fp)){
            $line.=fgets($fp, 1024);
        }
        fclose($fp);
    }
    return $line ;

}
$data="-----------------------------4358114262792\r\n"
."Content-Disposition: form-data; name=\"action\"\r\n\r\n".
"add\r\n".
"-----------------------------4358114262792\r\n".
"Content-Disposition: form-data; name=\"selectedTab\"\r\n\r\n".
"0\r\n".
"-----------------------------4358114262792\r\n".
"Content-Disposition: form-data; name=\"house[officeId]\"\r\n\r\n".
"450\r\n".
"-----------------------------4358114262792\r\n".
"Content-Disposition: form-data; name=\"house[streetId]\"\r\n\r\n".
"968432\r\n".
"-----------------------------4358114262792\r\n".
"Content-Disposition: form-data; name=\"house[number]\"\r\n\r\n".
"20\r\n".
"-----------------------------4358114262792\r\n".
"Content-Disposition: form-data; name=\"house[building]\"\r\n\r\n".
"1\r\n".
"-----------------------------4358114262792\r\n".
"Content-Disposition: form-data; name=\"house[sourceUrl]\"\r\n\r\n".
"http://uk-sozidanie.ru/homes/\r\n".
"-----------------------------4358114262792\r\n".
"Content-Disposition: form-data; name=\"house[statusId]\"\r\n\r\n".
"1\r\n".
"-----------------------------4358114262792\r\n".
"Content-Disposition: form-data; name=\"__token\"\r\n\r\n".
"cfc3901629435\r\n".
"-----------------------------4358114262792--";
...
$b = connect2("ospb.dev.1adw.com", "POST", "/vt/houses/add", $data ,$cookie);

And here is the error: 这是错误:

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\cdo\www\add.php on line 46

I'm just trying to simulate a real request that works fine and returns good result almost imidietly. 我只是想模拟一个真实的请求,该请求可以正常工作,并且几乎可以简单地返回良好的结果。

Added: Did I count my data length properly? 补充:我是否正确计算了数据长度?

At the top of your file, after the <?php tag, put set_time_limit(0); 在文件顶部的<?php标记之后,放置set_time_limit(0); for unlimited processing time... or use something higher, like 120 无限的处理时间...或使用更高的值,例如120

You should use cURL functions in PHP for all HTTP/HTTP requests, that should solve your problem. 您应该在PHP中对所有HTTP / HTTP请求使用cURL函数,这应该可以解决您的问题。

If you dont want to use cURL, pls post actual request/response headers from actual request that you are sending. 如果您不想使用cURL,请从您发送的实际请求中发布实际请求/响应标头。 Problem could be with the $vars data you are sending in it. 问题可能与您在其中发送的$ vars数据有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM