PHP curl 不发送 post 参数

PHP curl not sending post params

我有一个在线表单可以将数据提交为 http post。 如果没有数据作为 post 发送,它将 return 以下错误消息

We are sorry, the form that you have submitted is invalid or no longer exists.

我正在使用以下代码发送 http post 数据,使用 curl.But 我总是收到错误消息作为输出。 如何解决此问题 issue.Did 我错过了 anything.I 打印的 curl 请求,并且 post 参数不是 sent.How 我可以解决这个问题吗? 我的代码如下

function curlUsingPost($url, $data)
{
  $content_type = 'application/x-www-form-urlencoded';

    if(empty($url) OR empty($data))
    {
        return 'Error: invalid Url or Data';
    }

    
    //url-ify the data for the POST
    $fields_string = '';
    foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    $fields_string = http_build_query($data).'\n';
    echo $fields_string;


    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
 //   curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: ' . $content_type]);
 
 curl_setopt($ch, CURLOPT_HTTPHEADER,array($content_type));
    curl_setopt($ch,CURLOPT_URL,$url);
    
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt( $curl_handle, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); # timeout after 10 seconds, you can increase it
   //curl_setopt($ch,CURLOPT_HEADER,false);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  # Set curl to return the data instead of printing it to the browser.
   curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); # Some server may refuse your request if you dont pass user agent

   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
   
   
   $info = curl_getinfo($ch);
    
   print_r($info );
    //execute post
    $result = curl_exec($ch);



    //close connection
    curl_close($ch);
    return $result;
}


$url = 'office.promptip.com:8443/his1.max/Services/Webform.aspx?request=submitwebform&token=202D36560E444E466658461615120F75411B09634C696C6F79454E795E7C7A5154505645414F456C584C17101B443D431D0B654D66686D7C454E795E2D7E5152565341404145625E4213141E4273421E5E66486B666F2A464B7A5B79785F54520645444F15635C454516134422421A0D664A3D68687C454E79587F7D5F52575714424043635E1611181D16704D1D0E654F69696F2A474A7A0A78785807525E45414E406350424016134422421A0D374C6F686C7C44482F59787D5E530150104218426D584316181A4177411B0F';
//echo $url;
$data = [
'C0IFirstName' => 'John Doe',
'C1ILastName' => 'Doe John',
'C2IPosition' =>'Father-Guardian',
'U3I80'=>'8129020464',
'U4I150'=>'johndoe@johndoe.com',
'U5I79'=>'This is the message buddy',
'U6I102' => 'Ann',
'U7I105' =>'Doe',
'U8I52' =>'FS1',
'C9IClientId'=> rand ( 0,100000 ),
'U10I21'=>'Website-Enquiry Form'

];

echo curlUsingPost($url,$data);

解决方案

将此添加到您的 curl 请求中:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

并在您请求的 URL 中将 & 替换为 &

$url = 'office.promptip.com:8443/his1.max/Services/Webform.aspx?request=submitwebform&token=202D36560E444E466658461615120F75411B09634C696C6F79454E795E7C7A5154505645414F456C584C17101B443D431D0B654D66686D7C454E795E2D7E5152565341404145625E4213141E4273421E5E66486B666F2A464B7A5B79785F54520645444F15635C454516134422421A0D664A3D68687C454E79587F7D5F52575714424043635E1611181D16704D1D0E654F69696F2A474A7A0A78785807525E45414E406350424016134422421A0D374C6F686C7C44482F59787D5E530150104218426D584316181A4177411B0F';

解释

起初,您请求的 URL 有误,这可能是从某些 HTML 文档中复制的。通常,& 实体会被 HTML 解释器替换为 & 字符,但这不适用于 curl,您必须手动替换实体。

还有服务器returns

We are sorry, the form that you have submitted is invalid or no longer exists.

每个端点的每个请求都会得到响应 body。但对于成功的请求,它还提供了 Location header。通常浏览器会跟随这个位置​​,发出第二个请求并显示成功消息。你必须告诉 cURL 做与 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true).

完全相同的事情