drf16571 2015-04-17 08:33
浏览 50
已采纳

使用XML格式的Curl发布数据

Currently post my data in HTTP Post format and i need to post it in XML and add the header.

This is how i currently post my data.

//extract data from the post
extract($_POST);

//set POST variables
$url = 'https://test.com/checklead';
$fields = array(
                        'surname' => urlencode($surname),
                        'first_name' => urlencode($first_name),
                        'dob' => urlencode($dob),
                        'email' => urlencode($email),
                        'home_phone' => urlencode($home_phone),
                        'mobile_phone' => urlencode($mobile_phone),
                        'work_phone' => urlencode($work_phone),
                        'postcode' => urlencode($postcode),
                        'leadid' => urlencode(123),
                        'affid' => urlencode(123),
                        'subid' => urlencode(123),
                        'bank_acno' => urlencode($bank_acno),
                        'bank_sort' => urlencode($bank_sort),
                        'amount_required' => urlencode($amount_required),

                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

How do i post this in XML Format and add a Header : http://www.header.com

  • 写回答

1条回答 默认 最新

  • dongyinzheng6572 2015-04-17 08:43
    关注

    You've not set Content-Type. Try something along the lines of :

    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Header1: my_header_value'
        ));
    $output = curl_exec($ch);
    

    or you could look at using a library such as Guzzle which does a lot of the error handling etc for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?