卷曲如何接收标题和正文细节

时间:2015-02-13 04:17:23

标签: php curl

我正在测试一个curl代码,将一些数据发送到另一个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 ";
?>

这是我运行此脚本时获得的内容。我的问题是它一直给我“来自服务器的空回复”,我想打印curl(rece1.php)调用的标题和正文回复。如何解决这个问题?

* 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

1 个答案:

答案 0 :(得分:1)

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

<?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