douwei3863 2013-05-28 19:59
浏览 70
已采纳

使用PHP调用URL并将参数作为POST发送

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.

I have this scenario:

I invoke a Java Rest WS using the following url:

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:

<?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. 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. Thanks in advance. Best regards.

  • 写回答

1条回答 默认 最新

  • douyuepi6485 2013-05-28 20:29
    关注
    curl_setopt ($ch, CURLOPT_POSTFIELDS,'inputXml='.$xml);
    

    this is not going to work, you shoud url encode $xml first. (using urlencode function)

    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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源