简体   繁体   English

使用PHP调用URL并以POST形式发送参数

[英]Call an URL and send parameters as POST using PHP

I know this is maybe a very dummy question, but I'm facing a requirement with PHP, I've made some very simple things with it, but now I really need help. 我知道这可能是一个非常愚蠢的问题,但是我面对PHP的要求,为此我做了一些非常简单的事情,但是现在我确实需要帮助。

I have this scenario: 我有这种情况:

I invoke a Java Rest WS using the following url: 我使用以下网址调用Java Rest WS:

http://192.168.3.41:8021/com.search.ws.module.ModuleSearch/getResults/jsonp?xmlQuery=%3C?xml%20version%3D'1.0'%20encoding%3D'UTF-8'?%3E%3Cquery%20ids%3D%2216535%22%3E%3CmatchWord%3Ehave%3C/matchWord%3E%3CfullText%3E%3C![CDATA[]]%3E%3C/fullText%3E%3CquotedText%3E%3C!...

But for this I had to use a Java util class to replace some special chars in the xml parameter, because the original xml is something like: 但是为此,我不得不使用Java util类来替换xml参数中的一些特殊字符,因为原始的xml是这样的:

<?xml version='1.0' encoding='UTF-8'?><query ids="16914"><matchWord>avoir</matchWord><fullText><![CDATA[]]></fullText><quotedText><![CDATA[]]></quotedText><sensitivity></sensitivity><operator>AND</operator><offsetCooc>0</offsetCooc><cooc></cooc><collection>0</collection><searchOn>all</searchOn><nbResultDisplay>10</nbResultDisplay><nbResultatsParAspect>...

Now, I've been asked to create a PHP page in which I can set the XML as input and request it to the REST WS using a submit button. 现在,我被要求创建一个PHP页面,在其中可以将XML设置为输入,并使用提交按钮将其请求到REST WS。 I made an approach but not seems to be working, here I paste my code: 我采取了一种方法,但似乎没有用,在这里粘贴我的代码:

<?php
  if($_POST['btnSubmit'] == "Submit") 
  {
    $crudXmlQuery = $_POST['inputXml'];
    echo $crudXmlQuery;
    echo "=================================================";
    $xml = str_replace("%", "%25", $crudXmlQuery);
    $xml = str_replace("&", "%26", $crudXmlQuery);
    $xml = str_replace("=", "%3D", $crudXmlQuery);
    echo $xml;
    //$ch = curl_init($url);
    //curl_setopt ($ch, CURLOPT_POST, 1);
    //curl_setopt ($ch, CURLOPT_POSTFIELDS,'inputXml='.$xml);
    //$info = curl_exec ($ch);
    //curl_close ($ch);
  }
?>
<form action="sampleIndex.php" method="post">
    Please insert your XML Query
    <input type='text' name='inputXml' value='<?=$crudXmlQuery?>'/>

    <input type='submit' name='btnSubmit' value='Submit' />
</form>

I commented the part of the cURL since it was giving me some problems, I'm not sure how to handle this requirement yet, if somebody could help me please, I will really appreciate it. 我评论了cURL的一部分,因为它给我带来了一些问题,我不确定如何处理此要求,如果有人可以帮助我,我将非常感激。 Thanks in advance. 提前致谢。 Best regards. 最好的祝福。

curl_setopt ($ch, CURLOPT_POSTFIELDS,'inputXml='.$xml);

this is not going to work, you shoud url encode $xml first. 这行不通,您应该首先对$ xml进行url编码。 (using urlencode function) (使用urlencode函数)

The other part - not sure what exactly not working there :) But I do not see you taking the value of input field anywhere in your code: 另一部分-不确定到底什么不起作用:)但是我看不到您在代码中的任何地方都采用了输入字段的值:

$crudXmlQuery = $_POST['inputXml'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM