简体   繁体   中英

PHP XML Response not showing up in curl request

I'm testing an XML POST between two files:

  1. index.php, which writes out a simple xml to the page.

  2. send.php, which sends a curl POST to index.php and expects the xml response

The problem occurs when I open 'send.php' - the xml from index.php is not writing to the page. I suspect that I'm not writing out the xml properly, but could really use some guidance on this. 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 :

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

Besides, its is always good practice to use echo curl_error($curl); after curl_exec to track errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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