douqi4673 2012-08-06 18:43
浏览 59
已采纳

php - 如何能够ajax发布并从此脚本返回?

I'm quite stuck in how to edit this script be able to take a request from jquery and return to me the value the script returns? Any help is most gratefully appreciated?

I can use it to pass an xml file and url from the filesystem and get the return I expect but would really like to be able to use it as a proxy to a wsdl service.

[edit] Due to environmental changes I'm wanting to use the below script to pass back to me the xml return from a wsdl service that I'm passing xml. It works fine when I do so from the command line - I'm just unsure how to be able to get the script to accept a post and then return the xml return from the service call.

I'm using the script as follows from the command line:

php file.php theurl <test.xml

^^ Which returns to me the xml I would like to then pass back to the frontend

<?php
ini_set('display_errors', "1");

$url= $argv[1];

echo "url
$url
";

preg_match("/https?:\/\/([^\/]*)(.*)/", $url, $matches);
$host=$matches[1];
$request=$matches[2];

$mxml=fread(STDIN,65536);
$yt =curl_init();
$header =   "POST $request  HTTP/1.0
";
$header .=  "Host: $host
";
$header .=  "SoapAction:";
$header .=  "Content-Type: text/xml
";
$header .=  "Content-Length: ".strlen($mxml)."
";
$header .=  "Content-Transfer-Encoding: text
";
$header .=  "Connection-Close: close

";

echo "header
$header
";

$header .=  $mxml;

curl_setopt($yt, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($yt, CURLOPT_URL, $url);
curl_setopt($yt, CURLOPT_CUSTOMREQUEST, $header);
curl_setopt($yt, CURLOPT_RETURNTRANSFER, true);

$rxml=curl_exec($yt);
echo "sent
$mxml
";
echo "received
$rxml
";

echo curl_error($yt);
?>
  • 写回答

2条回答 默认 最新

  • duanliao3826 2012-10-27 17:37
    关注

    Sussed it. Sorry forgot to post answer. Hopefully help someone else:

    <?php
    $url = 'http://myURL';
    $xmlpost = file_get_contents('php://input');
    
    $header =   "POST $url  HTTP/1.0
    ";
    $header .=  "Host: myHOST
    ";
    $header .=  "SoapAction: ''";
    
    $ch = curl_init();
    
    //Set the URL
    curl_setopt($ch, CURLOPT_URL, $url);
    //Enable POST data
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlpost);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
    
    //curl_exec automatically writes the data returned
    $response = curl_exec($ch);
    curl_close($ch);
    echo $response;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误