如何通过卷曲发送XML等参数后在PHP [英] How to send XML and other post parameters via cURL in PHP

查看:150
本文介绍了如何通过卷曲发送XML等参数后在PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用低于code发送XML到我的REST API。 $ xml_string_data包含正确的XML,并且它被传递很好地mypi.php:

I've used code below to send XML to my REST API. $xml_string_data contains proper XML, and it is passed well to mypi.php:

    //set POST variables
    $url = 'http://www.server.cu/mypi.php'; 
    $fields = array(
                'data'=>urlencode($xml_string_data)
            );

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

    echo $fields_string;

    //open connection
    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_POST,count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
    curl_setopt($ch,CURLOPT_HTTPHEADER,array (
        "Expect: "
    ));

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

但是,当我添加了其他领域:

But when I've added other field:

    $fields = array(
      'method' => "methodGoPay",
      'data'=>urlencode($xml_string_data)
    );

据停止工作。在mypi.php我根本不收到任何更多的POST参数!

It stopped to work. On the mypi.php I don't recieve any more POST parameters at all!

你能请你告诉我该怎么做在一个卷曲的请求发送XML等岗位参数?

Could you you please tell me what to do to send XML and other post parameters in one cURL request?

请不要建议使用任何库,我wan't到acomplish它以纯PHP。

Please don't suggest using any libraries, I wan't to acomplish it in plain PHP.

推荐答案

我看不出什么毛病此脚本。这是最有可能mypi.php的问题。

I don't see anything wrong with this script. It's most likely an issue with mypi.php.

您确实有一个额外的放大器&;最后。也许这混淆了服务器?该RTRIM不改变$ field_string并返回修整字符串

You do have an extra & at the end. Maybe that confuses the server? The rtrim doesn't change the $field_string and it returns the trimmed string.

该postfields可以简化这个样子,

The postfields can be simplified like this,

$fields = array(
      'method' => "methodGoPay",
      'data'=> $xml_string_data // No encode here
);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));

这篇关于如何通过卷曲发送XML等参数后在PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆