简体   繁体   English

PHP XML响应未显示在curl请求中

[英]PHP XML Response not showing up in curl request

I'm testing an XML POST between two files: 我正在测试两个文件之间的XML POST:

  1. index.php, which writes out a simple xml to the page. index.php,它将简单的xml写入页面。

  2. send.php, which sends a curl POST to index.php and expects the xml response send.php,它将curl POST发送到index.php,并期望xml响应

The problem occurs when I open 'send.php' - the xml from index.php is not writing to the page. 当我打开'send.php'时会发生问题-index.php中的xml没有写入页面。 I suspect that I'm not writing out the xml properly, but could really use some guidance on this. 我怀疑我没有正确地写出xml,但是确实可以使用一些指导。 My code is below: 我的代码如下:

// send.php
$url_request = 'index.php';

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url_request);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
    $response = curl_exec($curl);
    curl_close($curl);

    $xml = simplexml_load_string($response);
    print_r($xml);

// index.php
header("Content-type: text/xml");

function sendResponse(){
    $response = '<?xml version="1.0" encoding="utf-8"?><response>success</response>';
    echo $response;
}

sendResponse();

You need to use the full path of the index.php file : 您需要使用index.php文件的完整路径:

$url_request = 'http://localhost/path/to/index/index.php';

Besides, its is always good practice to use echo curl_error($curl); 此外,使用echo curl_error($curl);永远是一个好习惯echo curl_error($curl); after curl_exec to track errors. curl_exec之后跟踪错误。

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

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