curl 如何接收 header 和 body 详细信息

Curl how to receive header and body details

我正在测试将一些数据发送到另一个 php 页面的卷曲代码。 下面是我用来发送 curl 请求的代码。

$xml_data = "<Request><NewOrder></NewOrder></Request>";


$URL = "http://127.0.0.1/test1/rece1.php";
$header ="";
// Build header as array for cURL option
$header = "HTTP/1.0\r\n";
$header.= "MIME-Version: 1.0\r\n";
//$header.= "Content-type: application/PTI46\r\n";
$header.= "Content-length: ".strlen($xml_data)."\r\n";
$header.= "Content-transfer-encoding: text\r\n";
$header.= "Request-number: 1\r\n";
$header.= "Document-type: Request\r\n";
//$header.= "Interface-Version: Test 1.4\r\n";
$header.= "Connection: close \r\n\r\n";              
$header.= $xml_data;   

// Define cURL options, then connect to server while saving response
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$URL);

curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_getinfo($ch, CURLINFO_HTTP_CODE);
//$f = fopen('request.txt', 'w');
curl_setopt($ch,CURLOPT_VERBOSE,1);
//curl_setopt($ch,CURLOPT_STDERR ,$f);

$rawResponse = curl_exec($ch);  
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
echo "CT : ".$content_type;
if (curl_errno($ch)) {
   print curl_error($ch);
} else {
   curl_close($ch);
}

这是我在 rece1.php

中所做的
<?php
echo "TEST ONE ";
?>

这是我 运行 这个脚本时得到的结果。我的问题是它一直给我 "Empty reply from server",我想打印 curl(rece1.php) 调用的 header 和 body 回复。如何解决这个问题?

* About to connect() to 127.0.0.1 port 80 (#0)
*   Trying 127.0.0.1... * connected
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> HTTP/1.0
MIME-Version: 1.0
Content-length: 40
Content-transfer-encoding: text
Request-number: 1
Document-type: Request
Connection: close

<Request><NewOrder></NewOrder></Request> /test1/rece1.php HTTP/1.1
Host: 127.0.0.1
Accept: */*

* Empty reply from server
* Connection #0 to host 127.0.0.1 left intact
CT : Empty reply from server
* Closing connection #0

将您的代码更新为以下内容:

<?php
$xml_data = "<Request><NewOrder></NewOrder></Request>";


$URL = "http://127.0.0.1/test1/rece1.php";

// move all your headers to the $headers array below
/*
    $header.= "MIME-Version: 1.0\r\n";
    //$header.= "Content-type: application/PTI46\r\n";
    $header.= "Content-length: ".strlen($xml_data)."\r\n";
    $header.= "Content-transfer-encoding: text\r\n";
    $header.= "Request-number: 1\r\n";
    $header.= "Document-type: Request\r\n";
    //$header.= "Interface-Version: Test 1.4\r\n";
    $header.= "Connection: close \r\n\r\n";  
*/
$headers = array(
    'MIME-Version: 1.0',
    'Content-transfer-encoding: text',
    'Request-number: 1',
    'Document-type: Request',
    'Content-type: application/xml', 
    'Content-length: ' . strlen($xml_data),
    'Connection: close',
);

// Define cURL options, then connect to server while saving response
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$URL);

curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);


//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_getinfo($ch, CURLINFO_HTTP_CODE);
//$f = fopen('request.txt', 'w');
curl_setopt($ch,CURLOPT_VERBOSE,1);
//curl_setopt($ch,CURLOPT_STDERR ,$f);

$rawResponse = curl_exec($ch);  
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
echo "CT : ".$content_type;
if (curl_errno($ch)) {
   print curl_error($ch);
} else {
   curl_close($ch);
}

CURLOPT_CUSTOMREQUEST 是一种请求方法,如 'POST'、'DELETE' 等。您可以在此处找到更多信息 http://php.net/manual/en/function.curl-setopt.php