如何在 PHP 中通过 cURL 发送 XML 和其他 post 参数 [英] How to send XML and other post parameters via cURL in PHP

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

问题描述

我使用下面的代码将 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!

你能告诉我如何在一个 cURL 请求中发送 XML 和其他 post 参数吗?

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

请不要建议使用任何库,我不想用纯 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.

后域可以这样简化,

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

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

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

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