简体   繁体   English

尝试将php文件中的http get请求发送到服务器并收到http响应

[英]Trying to send a http get request from a php file to a server and receive a http response

I'm trying to send a http get request in a php file to a server and receive the response. 我正在尝试将php文件中的http get请求发送到服务器并接收响应。

function broadcastRequest($catCommand,$command,$prop){
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL,'http://'.$prop["ipServer"].':'.$prop["portVLM"].$prop["startURL"].$catCommand."%20".$prop["broadcastName"].'%20'.$command);
    curl_setopt ($ch, CURLOPT_HEADER, 0);
    $res = curl_exec ($ch);
    $res = simplexml_load_file($res);   
    curl_close ($ch);
}
  • If I echo the first $res I can read the number 1 如果回显第一个$res我可以读取数字1
  • the second $res won't load because simplexml_load_file() isn't syntax-highlighted in my Notepad++ 第二个$res不会加载,因为我的Notepad ++中的simplexml_load_file()语法未突出显示

Can someone explain to me what I'm doing wrong, or advise another way of working? 有人可以向我解释我在做错什么,还是建议另一种工作方式?

First of all, Notepad++'s syntax highlighting has absolutely nothing to do with whether it's valid. 首先,Notepad ++的语法高亮与它是否有效完全无关 It just reads names, and you'll notice that simplexml_load_file is never highlighted. 它只是读取名称,您会注意到simplexml_load_file从未突出显示。

Now onto the problem. 现在到问题了。 $res is just a boolean value returned by curl_exec , because you haven't set the CURLOPT_RETURNTRANSFER option, as specified in the docs. $res只是curl_exec返回的布尔值,因为您没有按照文档中的指定设置CURLOPT_RETURNTRANSFER选项。 However, a simpler way is just loading it directly: 但是,一种更简单的方法是直接加载它:

function broadcastRequest($catCommand,$command,$prop) {
    $res = simplexml_load_file('http://' . $prop["ipServer"] . ':' . $prop["portVLM"] . $prop["startURL"] . $catCommand . '%20' . $prop["broadcastName"] . '%20' . $command);
}

If you'd like to use cURL, however, just set the additional option: 但是,如果您想使用cURL,只需设置其他选项即可:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Looks like you need to set the CURLOPT_RETURNTRANSFER option. 看起来您需要设置CURLOPT_RETURNTRANSFER选项。 However, it's probably easier to just do this: 但是,这样做可能会更容易:

$res = file_get_contents($uri);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM