php http GET命令不起作用

标签 php linux apache http get

你好,
我有一个以太网设备,我可以通过浏览器或 Linux Curl 毫无问题地查询,这是我使用 curl 得到的响应。响应采用简单的 XML 表示法。让设备回答的 URL 是
http://mydevice.com/state.xml?noReply=0

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

但是当我使用这个 PHP 代码时它不起作用,这个代码中有一些调试信息。如果我将代码指向 www.example.com,代码就可以工作,我已经用尽了谷歌搜索,没有任何提示等......
我什至简化了 PHP http 数组,只使用 Linux Curl 使用的东西,但设备仍然没有答案。
请指教。

---------------------PHP 代码 ------------
<!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>

最佳答案

您可能需要考虑使用 cURL PHP 扩展。这是相当普遍的票价,它为您提供比核心流包装器更多的保护措施。

引用页:https://php.net/curl

还有一个简单的代码示例 GET ,取自评论:

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

您有一系列 curl 选项来设置标题、cookie 等(通过 curl_setopt )。看看文档,绝对值得你花时间。无需盲目飞行。

关于php http GET命令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22774150/

相关文章:

PHP 包含当 "root forward slash"在包含路径之前,例如 : "/folder/file.php"

Php按值或按引用传递

php - mysql导出后找不到文件

php - 如何配置 docker 在单独的容器中使用 apache 和 php

php - UTF-8贯穿始终

php - new_link=TRUE 和 sql.safe_mode = OFF 时多个数据库连接不起作用

php - 将 pdf 作为 blob 插入并显示

php - 为什么 Apache 像 PHP 一样执行 .php.html 文件?

linux - 如何阻止访问者直接访问我网站中的目录?

c - 在哪里可以找到信号和警报功能定义?