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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看