代码之家  ›  专栏  ›  技术社区  ›  Brian Bruman

从外部PHP类返回用于If条件的值

  •  2
  • Brian Bruman  · 技术社区  · 7 年前

    仍在学习OOP编程,但我正在使用 proxy-scraper

    test.php 我希望从外部php文件中的类返回变量的脚本:

    <?php
    
    use GuzzleHttp\Client as GuzzleClient;
    use Vantoozz\ProxyScraper\HttpClient\GuzzleHttpClient;
    use Vantoozz\ProxyScraper\Scrapers;
    
    $scraper = new Scrapers\HideMyIpScraper($httpClient);
    
    foreach ($scraper->get() as $proxy2) {
    
        $proxyexplode = explode(':', $proxy2);
        $ipv4proxy = (string)$proxyexplode [0];
        $portproxy = (int)$proxyexplode [1];
    
        $proxy = new Proxy(new Ipv4($ipv4proxy), new Port($portproxy));
    
    }
    

    现在,它与本地主机上的文件交互 Scrapers/HideMyIpScraper.php . 可以查看整个php文件 here

    无论何时 $proxy

    /**
     * @return \Generator|Proxy[]
     * @throws \Vantoozz\ProxyScraper\Exceptions\ScraperException
     */
    public function get(): \Generator
    {
        try {
            $html = $this->httpClient->get($this->makeUrl());
        } catch (HttpClientException $e) {
            throw new ScraperException($e->getMessage(), $e->getCode(), $e);
        }
    
        foreach ($this->extractData($html) as $item) {
    
            $countrycode = $item['c']['f'];
            var_dump($countrycode);
    
            if (!\is_array($item)) {
                continue;
            }
            try {
                yield $this->makeProxy($item);
            } catch (InvalidArgumentException $e) {
                continue;
            }
        }
    }
    

    我在上面添加的唯一内容是 $countrycode = $item['c']['f'];

    test.php foreach 循环,所以是这样的

    $array = array();
    foreach ($scraper->get() as $proxy2) {
        $proxy = new Proxy(new Ipv4($ipv4proxy), new Port($portproxy));
        // $countrycode is var_dumped here from `HideMyIpScraper.php`
        if ($countrycode == 'us') {
            $array[] = $proxy2;
        }
    

    但当然 $countrycode 在另一个php文件的外部函数中,因此这是无效的,并抛出一个错误。

    return $countrycode 但这打破了传统 foreach ($this->extractData($html) as $item) { 在第一次迭代后循环。

    我怎样才能拿到票 $countrycode 变量在我的 test.php 条件逻辑脚本?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Barmar    7 年前

    从生成器返回包含代理和国家/地区代码的数组。

    public function get(): \Generator
    {
        try {
            $html = $this->httpClient->get($this->makeUrl());
        } catch (HttpClientException $e) {
            throw new ScraperException($e->getMessage(), $e->getCode(), $e);
        }
    
        foreach ($this->extractData($html) as $item) {
    
            $countrycode = $item['c']['f'];
            var_dump($countrycode);
    
            if (!\is_array($item)) {
                continue;
            }
            try {
                yield array($this->makeProxy($item), $countrycode);
            } catch (InvalidArgumentException $e) {
                continue;
            }
        }
    }
    

    然后将结果分成两部分 foreach

    $array = array();
    foreach ($scraper->get() as $val) {
        list ($proxy2, $countrycode) = $val;
        $proxy = new Proxy(new Ipv4($ipv4proxy), new Port($portproxy));
        // $countrycode is var_dumped here from `HideMyIpScraper.php`
        if ($countrycode == 'us') {
            $array[] = $proxy2;
        }
    
    推荐文章