【问题标题】:php http GET command not workingphp http GET命令不起作用
【发布时间】:2014-05-11 12:49:33
【问题描述】:

你好, 我有一个以太网设备,我可以通过浏览器或 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>

【问题讨论】:

  • 在我看来好像错过了:$http_response_header = $myURL = file_get_contents($myURL); 在最后一行之前。
  • 对不起,我是 PHP 新手,您能解释一下您的意思吗?我将我的示例 php 代码放在在线阅读 PHP 手册中。 “$http_response_header = $myURL = file_get_contents($myURL);”这一行在哪里?去,它有什么作用?
  • 如果您可以使用 Linux Curl 命令查询设备,您应该可以在 PHP 中使用 PHP 的 curl 扩展名进行查询。其他包装器是否可以工作取决于您的 PHP 配置。如果你没有得到答案,也许 PHP 没有按照你的想法做。查看服务器错误日志。

标签: php linux apache http get


【解决方案1】:

您可能需要考虑使用 cURL PHP 扩展。这是很常见的一种方式,它比核心流包装器为您提供更多的保护。

参考页面:https://php.net/curl

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

<?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)。只需看一下文档,这绝对值得您花时间。无需盲目飞行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2015-04-24
    • 2020-09-15
    • 2021-08-12
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多