PHP卷曲不发送邮件参数

时间:2015-08-13 09:29:45

标签: php curl http-post

我有一个在线表单以http post的身份提交数据。 如果没有数据作为帖子发送,它将返回以下错误消息

  

很抱歉,您提交的表单无效或不再存在。

我使用以下代码使用curl发送http post数据。但是我总是将错误消息作为输出。 如何解决这个问题。我错过了什么。我打印了卷曲请求,并没有发送帖子参数。我该如何解决这个问题? 我的代码如下:

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);

1 个答案:

答案 0 :(得分:1)

解决方案

将此添加到您的卷曲请求中:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

&替换为您要求的网址中的&

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

Explenation

首先,您要求提供错误的网址,这可能是从某些HTML文档中复制的。通常情况下,&实体将由HTML解释器替换为& char,但这不适用于curl,您必须手动替换实体。

服务器也返回

  

很抱歉,您提交的表单无效或无效   存在的时间越长。

每个结束响应正文中的每个请求。但是对于成功请求,它还提供Location标头。通常,浏览器会跟随此位置,发出第二个请求并显示成功消息。您必须告诉cURL与curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true)完全相同。