如果这是一个重复的问题,请告诉我,但这开始变得荒谬了。
我想用 PHP:
GET http://www.example.com/hello.xyz
并将此标头添加到请求中:
"X-Header-Name: $foobar"
其中 foobar 来自已定义的 php 变量。
and then store the response in a variable. That's it! Nothing more, nothing less.
But I can't find it!
I don't want to use curl or anything like that, it'd slow it down too much if I run curl everytime. Edit: My main concern with using curl is about compatibility with Windows (local server) vs. Linux (deployment server).
<?php
echo "So, how do I do it in the simplest way possible?";
?>
您可以使用文件获取内容 http://php.net/manual/en/function.file-get-contents.php如果你不想使用curl http://www.php.net/manual/en/book.curl.php但不确定速度但它是php
的内置函数其中curl
不是。当谈到speed
那么我认为无论您使用什么用于远程请求,速度/性能将更多地取决于网络连接速度而不是函数/库,也许这些之间有一点不同(curl/file_get_contents/fsockopen),但我认为它会很小(1-2%),你就看不出差异,看起来几乎一样。
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"X-Header-Name: $foobar"
));
$context = stream_context_create($opts);
$data = file_get_contents('http://www.example.com/hello.xyz', false, $context);
if($data) {
// do something with data
}
另外,如果你想使用curl
那么你可以用这个
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Header-Name: $foobar"));
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/hello.xyz");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($curl_errno == 0) {
// $data received in $data
}
Also, 检查这个答案 https://stackoverflow.com/questions/11064980/php-curl-vs-file-get-contents,它可能会帮助您做出决定。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)