在 PHP 中执行此 cURL

发布于 2025-01-03 09:25:03 字数 375 浏览 0 评论 0原文

如何在 PHP 中完成此操作

curl -I http://www.google.com | grep "Server:"

?是否可以仅回显 Linux 等操作系统或网站的 Windows,例如使用curl 回显 Web 服务器

My Try

<?
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,"http://google.com");
curl_setopt ($ch, CURLOPT_HEADER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
echo $data  ;

?>

how can this be done in PHP

curl -I http://www.google.com | grep "Server:"

and is it possible to just echo OS like Linux Or windows of site , like web server is echoed using curl

My Try

<?
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,"http://google.com");
curl_setopt ($ch, CURLOPT_HEADER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
echo $data  ;

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糯酥胸 2025-01-10 09:25:03
<?php
// create a new cURL resource                                                                                                                                
$ch = curl_init();

// set URL and other appropriate options                                                                                                                     
curl_setopt($ch, CURLOPT_URL, "http://www.google.com/");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab URL and pass it to the browser                                                                                                                       
$result = curl_exec($ch);

/* Curl returns multiple headers, if the last action required multiple                                                                                       
 * requests, e.g. when doing Digest authentication. Only parse the                                                                                           
 * headers of the latest response. */
preg_match_all('/(^|\r\n\r\n)(HTTP\/)/', $result, $matches, PREG_OFFSET_CAPTURE);
$startOfHeaders = $matches[2][count($matches[2]) - 1][1];
$endOfHeaders = strpos($result, "\r\n\r\n", $startOfHeaders);
$headers = substr($result, $startOfHeaders, $endOfHeaders - $startOfHeaders);
$headers = preg_split("/\r?\n/", $headers);
foreach ($headers as $headerLine) {
  if (preg_match('|^Server:\s+(.+)|', $headerLine, $m)) {
    var_dump($m[1]);
  }
}

// close cURL resource, and free up system resources                                                                                                         
curl_close($ch);

基本上,这是Horde_Http 库的摘录。具体来说Curl 请求Curl 响应 处理程序。

它比您使用“grep”命令所做的要复杂一些,但看起来您想分析响应标头。这就是为什么我在代码中留下了更复杂的标头分析。

<?php
// create a new cURL resource                                                                                                                                
$ch = curl_init();

// set URL and other appropriate options                                                                                                                     
curl_setopt($ch, CURLOPT_URL, "http://www.google.com/");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab URL and pass it to the browser                                                                                                                       
$result = curl_exec($ch);

/* Curl returns multiple headers, if the last action required multiple                                                                                       
 * requests, e.g. when doing Digest authentication. Only parse the                                                                                           
 * headers of the latest response. */
preg_match_all('/(^|\r\n\r\n)(HTTP\/)/', $result, $matches, PREG_OFFSET_CAPTURE);
$startOfHeaders = $matches[2][count($matches[2]) - 1][1];
$endOfHeaders = strpos($result, "\r\n\r\n", $startOfHeaders);
$headers = substr($result, $startOfHeaders, $endOfHeaders - $startOfHeaders);
$headers = preg_split("/\r?\n/", $headers);
foreach ($headers as $headerLine) {
  if (preg_match('|^Server:\s+(.+)|', $headerLine, $m)) {
    var_dump($m[1]);
  }
}

// close cURL resource, and free up system resources                                                                                                         
curl_close($ch);

Basically this is an extract from the Horde_Http library. Specifically the Curl request and the Curl response handlers.

It is somewhat more complex than what you did with your "grep" command but it looked as though you want to analyse the response headers. That is why I left the somewhat more complex header analysis in the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文