请帮助修复 php/api/curl 代码 [英] Help fixing php/api/curl code please

查看:22
本文介绍了请帮助修复 php/api/curl 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有的是一个域可用性检查,它连接到一个 API 并从 $tmp 输出可用:和不可用:".下面的代码只会检查可用性一次.

What I have in place, is a domain availability check, which connects up to an API and outputs "Available: and Unavailable:" from $tmp. Ths below code will only check the availability ONCE.

我想多次检查域的可用性(可能在循环中?),不必每次都运行重新启动 cURL 连接(因为它浪费时间 - 每个查询 300 毫秒到 1 秒).

I would like to check the availability of the domain, multiple times (possibly on a loop?), without having to run restart cURL connection everytime (as it wastes time - 300ms to 1s per query).

我只是不知道如何连接到 cURL 一次并运行循环(通过 API 进行检查).非常感谢帮助调整代码!最大限度地减少输出可用/不可用"和循环检查所需的时间是关键.

I just don't know how I can connect to cURL once and run the loop (doing the check through the API). Help adjusting the code would be very much appreciated! Minimizing the time it takes to output "available/not available" and looping the checks is key.

谢谢.

当前代码

<?php

    function GetCurlPage ($pageSpec)
    {
      $ch = curl_init($pageSpec);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      $tmp = curl_exec ($ch);
      curl_close ($ch);
      $tmp = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $tmp);
      $tmp = explode('<br>', $tmp);
      echo $tmp[0];
      echo "<br>";
      echo $tmp[1];
      echo "<br>";
      return $tmp;
    }

$returnUrl = "http://www.mysite.com.au/check.php";
$url = "https://www.apisite.com.au/availability/check.php?domain=testdomain&suffixes=.com.au";
$output = GetCurlPage("$url");

?>

@Marc B

    function getCurlPage($pageSpec) {
if (is_null($ch)) {
    $ch = curl_init($pageSpec);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
} else {
    curl_setopt($ch, CURLOPT_URL, $pageSpec);
}
  while ($i < 5) {
  $tmp = curl_exec ($ch);
  //curl_close ($ch);
  $tmp = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $tmp);
  $tmp = explode('<br>', $tmp);
  echo $tmp[0];
  echo "<br>";
  echo $tmp[1];
  echo "<br>";
  echo udate('H:i:s:u');
  echo "<br><br>";
  $i++;
  }
      return $tmp;
}

推荐答案

这应该可以回答您的问题:使用 PHP Curl 库保持 HTTP 持久/保持连接?

This should answer your question: Persistent/keepalive HTTP with the PHP Curl library?

评论跟进:

function getCurlPage($pageSpec) {
    if (is_null($ch)) 
        static $ch = curl_init($pageSpec);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    } else {
        curl_setopt($ch, CURLOPT_URL, $pageSpec);
    }
    $tmp = curl_exec($ch);
    ... do NOT close the curl handle, otherwise do the rest the same as before ...
}

可能不会按原样工作,在我头顶上做这件事并且只睡了 2 小时,但这应该足以让你开始.

Probably won't work as is, doing this off the top of my head and with only 2 hours sleep, but this should be enough to get you started.

顺便说一句,没有必要为 GetCurlPage("$url") 做双引号,这是在浪费解析器时间,因为 PHP 将不得不创建一个新的空字符串,东西 $url 放入其中,并将新字符串向下传递.只需执行 GetCurlPage($url).

And by the way, there's no need to do doublequotes for GetCurlPage("$url"), it's a waste of parser time, as PHP will have to create a new empty string, stuff $url into it, and pass the new string on down. Just do GetCurlPage($url).

这篇关于请帮助修复 php/api/curl 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆