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

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败