php http GET命令不工作
你好,, 我有一个以太网设备,可以通过浏览器或LinuxCurl进行查询,没有问题,这就是我使用Curl得到的响应。响应采用简单的XML表示法。获取要应答的设备的URL为php http GET命令不工作,php,linux,apache,http,get,Php,Linux,Apache,Http,Get,你好,, 我有一个以太网设备,可以通过浏览器或LinuxCurl进行查询,没有问题,这就是我使用Curl得到的响应。响应采用简单的XML表示法。获取要应答的设备的URL为 dave@server12:/home/dave/www$curl-v-0http://mydevice.com/state.xml?noReply=0 *即将连接()到mydevice.com端口80(#0) *正在尝试mydevice.com。。。有联系的 >GET/state.xml?noReply=0 HTTP/1
dave@server12:/home/dave/www$curl-v-0http://mydevice.com/state.xml?noReply=0
*即将连接()到mydevice.com端口80(#0)
*正在尝试mydevice.com。。。有联系的
>GET/state.xml?noReply=0 HTTP/1.0
>用户代理:curl/7.22.0(i686 pc linux gnu)libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
>主持人:mydevice.com
>接受:*/*
>
0
0
0
0
*0到主机mydevice.com的连接保持不变
*正在关闭连接#0
但是当我使用这段PHP代码时,它不起作用,代码中有一些调试信息。如果我把它指向www.example.com,代码就可以运行了,我已经用尽了谷歌搜索,没有任何提示等等。。。 我甚至简化了PHP http数组,只使用LinuxCurl使用的东西,但设备仍然没有给出答案。 请告知 ---------------------PHP代码------------
你可能想考虑使用CURL PHP扩展。这是一种非常普通的方式,它比核心流包装器提供了更多的保护
参考页:
还有一个简单的GET
的小代码示例,摘自注释:
您有一系列curl选项来设置标题、cookie等(通过curl\u setopt
)。看看医生,这绝对值得你花时间。无需盲目飞行。在我看来,似乎没有什么可做的:$http\u response\u header=$myURL=file\u get\u contents($myURL)代码>在最后一行之前。对不起,我是PHP新手,你能解释一下你的意思吗?我把阅读php手册的php代码示例放到了网上。行“$http\u response\u header=$myURL=file\u get\u contents($myURL);”去哪里了?它做什么了?如果您可以使用Linux Curl命令查询设备,那么您应该可以使用PHP的Curl
扩展从PHP内部进行查询。其他包装器是否工作取决于您的PHP配置。如果你没有得到答案,也许PHP没有按照你的想法去做。查看服务器错误日志。
dave@server12:/home/dave/www$ curl -v -0 http://mydevice.com/state.xml?noReply=0
* About to connect() to mydevice.com port 80 (#0)
* Trying mydevice.com... connected
> GET /state.xml?noReply=0 HTTP/1.0
> User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: mydevice.com
> Accept: */*
>
<?xml version="1.0" encoding="utf-8"?>
<datavalues>
<relaystate>0</relaystate>
<inputstate>0</inputstate>
<rebootstate>0</rebootstate>
<totalreboots>0</totalreboots>
* Connection #0 to host mydevice.com left intact
* Closing connection #0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
$xml_status = 'http://mydevice.com/state.xml?noReply=0';
$test1 = 'http://www.example.com/';
$myURL = $xml_status;
//$myURL = $test1;
function aa_dump($type = "NoNameGiven", $input1)
{
echo "<pre>\r\n";
echo "---DEBUG-------- Printing $type ---------------\r\n";
var_dump($input1);
echo "\r\n--------------------------\r\n";
}
// this will break the URL into
// [scheme] => http
// [host] => www.example.com
// [path] => /foo/bar
// [query] => hat=bowler&accessory=cane
$aa_arrayURL = parse_url($myURL);
aa_dump("URL structure", $aa_arrayURL);
$aa_host = $aa_arrayURL['host'];
$aa_path = $aa_arrayURL['path'];
$aa_query = $aa_arrayURL['query'];
file_get_contents($myURL);
aa_dump("file_get_content $myURL", $http_response_header);
//$postdata = http_build_query( array( 'noReply' => '0'));
//aa_dump('postdata', $postdata);
$params = array('http' => array( 'method' => "GET",
'header' => array("Host: $aa_host",
"Content-Type: text/html",
"Accept: */*",
"Accept-Encoding: gzip, deflate",
"Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7",
"Accept-Language: en-US",
"Connection: close",
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"),
'timeout' => '2',
'max_redirects' => '0',
'ignore_errors' => '1',
'Request_fulluri' => 'TRUE',
'content' => "{$aa_path}?{$aa_query}" )
);
aa_dump('params', $params);
// workaround for php bug where http headers don't get sent in php 5.2
if(version_compare(PHP_VERSION, '5.3.0') == -1){
ini_set('user_agent', 'PHP-SOAP/' . PHP_VERSION . "\r\n" . $params['http']['header']);
}
$context = stream_context_create($params);
$homepage = file_get_contents($myURL, false, $context);
aa_dump('homepage', $homepage);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Status</title>
</head>
<body>
<hr />
<p>In body of HTML now</p>
<p>Status...</p>
<?php
print("<p>headers_list</p>");
var_dump(headers_list());
print("<p>homepage</p>");
if( strlen($homepage) == 0 )
{
echo '<p><font color="red">string came back empty</font></p>';
}
else
{
echo '<p>string has value</p>';
$xml = simplexml_load_string($homepage);
echo '<pre>homepage: '.$homepage.'</pre>';
echo '<pre>XML 1: '.$xml.'</pre>';
}
?>
</body>
</html>
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// Check if any error occurred
if(curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
// close curl resource to free up system resources
curl_close($ch);
?>