PHP-使用无卷曲http web请求将XML数据发布到网关
我试图找到一种无卷曲的方式,通过http web请求将数据发布到第三方支付网关。我开发了下面的代码,它实际上成功地与接收页面进行了通信,但显然没有发送POST数据PHP-使用无卷曲http web请求将XML数据发布到网关,xml,http-headers,httpwebrequest,file-get-contents,postdata,Xml,Http Headers,Httpwebrequest,File Get Contents,Postdata,我试图找到一种无卷曲的方式,通过http web请求将数据发布到第三方支付网关。我开发了下面的代码,它实际上成功地与接收页面进行了通信,但显然没有发送POST数据 <?php function makeWebRequest() { //URL where to send the data via POST $url = 'http://localhost/connect_to_gateway/receive.php'; //the actual data
<?php
function makeWebRequest()
{
//URL where to send the data via POST
$url = 'http://localhost/connect_to_gateway/receive.php';
//the actual data
$xml = '<?xml version="1.0" encoding="UTF-8"?>' .
'<test>' .
'Hi there!' .
'</test>';
$data = array('xml' => $xml);
//prepare the HTTP Headers
$content_type = 'text/xml';
$data = http_build_query($data);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: ' . addslashes($content_type) .'\r\n'
. 'Content-Length: ' . strlen($data) . '\r\n',
'content' => $data,
),
);
$context = stream_context_create($options);
/*send the data using a cURL-less method*/
$result = file_get_contents($url, false, $context);
echo 'file_get_contents<br/>';
var_dump($result); /* <=== POST DATA NOT ARRIVING AT DESTINATION (I only receive a string with "START- FINISH - ") */
echo '<br/><br/><br/><br/>';
}
//call the function
makeWebRequest();
?>
任何帮助都将不胜感激
对于那些问我为什么要避免使用cURL的人来说,这是因为libcurl并不总是安装在每个人的服务器上,我正试图找到一种方法来实现同样的目的,而不必要求管理员安装libcurl(如果libcurl不是在原始安装中安装的话)。。我正在尝试使用“香草php”
其他类似的stackoverflow帖子没有帮助:
- 我找到了解决问题的方法:)
我发现,当内容类型不是application/x-www-form-urlencoded时,PHP中的$\u POST将没有任何数据可读取。相反,应该访问$HTTP\u RAW\u POST\u数据
因此,通过文件\获取\内容通过POST发送xml数据的代码如下:
<?php
function makeWebRequest()
{
//URL where to send the data via POST
$url = 'http://localhost/connect_to_gateway/receive.php';
//the actual data
$xml = '<?xml version="1.0" encoding="UTF-8"?>' .
'<test>' .
'Hi there!' .
'</test>';
//prepare the HTTP Headers
$content_type = 'text/xml';
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: ' . addslashes($content_type) .'\r\n'
. 'Content-Length: ' . strlen($xml) . '\r\n',
'content' => $xml,
),
);
$context = stream_context_create($options);
/*send the data using a cURL-less method*/
$result = file_get_contents($url, false, $context);
echo 'file_get_contents<br/>';
var_dump($result);
}
//call the function
makeWebRequest();
?>
if ($HTTP_RAW_POST_DATA)
{
//create an xml parser and attempt to read it outputting errors if any
$xml_parser=xml_parser_create();
if(!xml_parse_into_struct($xml_parser, $HTTP_RAW_POST_DATA, $vals, $index))
var_dump(array("ERROR"=>sprintf("XML error: %s at line %d",xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser))));
}
谢谢大家。。呸。。那太痛苦了
<?php
function makeWebRequest()
{
//URL where to send the data via POST
$url = 'http://localhost/connect_to_gateway/receive.php';
//the actual data
$xml = '<?xml version="1.0" encoding="UTF-8"?>' .
'<test>' .
'Hi there!' .
'</test>';
//prepare the HTTP Headers
$content_type = 'text/xml';
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: ' . addslashes($content_type) .'\r\n'
. 'Content-Length: ' . strlen($xml) . '\r\n',
'content' => $xml,
),
);
$context = stream_context_create($options);
/*send the data using a cURL-less method*/
$result = file_get_contents($url, false, $context);
echo 'file_get_contents<br/>';
var_dump($result);
}
//call the function
makeWebRequest();
?>
if ($HTTP_RAW_POST_DATA)
{
//create an xml parser and attempt to read it outputting errors if any
$xml_parser=xml_parser_create();
if(!xml_parse_into_struct($xml_parser, $HTTP_RAW_POST_DATA, $vals, $index))
var_dump(array("ERROR"=>sprintf("XML error: %s at line %d",xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser))));
}