将$ sSymbol输入参数传递给类

时间:2016-04-08 21:04:14

标签: php

我从https://github.com/ricardotiago/sec-gov-api获得了此类代码,该代码用于检索和解析美国证券交易所网站的季度和年度报告。我不明白如何打印结果以及它的参数类型。 ; s接受。 $ sSmbol是输入参数,如何传递此参数以显示季度和年度报告

   <?php
    require_once "parallelCurl.php";
    class SecGovAPI {
      const SEARCH_LINK = "http://www.sec.gov/cgi-bin/browse-edgar";
      const BASE_LINK = "http://www.sec.gov/";
      const QUARTERLY_REPORT = '10-Q';
      const YEARLY_REPORT = '10-K';
      const MOST_RECENT_REPORT = 0;
      const ALL_REPORTS = 1;
      public function __construct($sSymbol) {
        $this->oParallelCurl = new ParallelCurl();
        $this->sSymbol = $sSymbol;
        $this->oDoc = new DOMDocument();
        $this->report = array();
      }
      public function getQuaterlyReport($iSearchType = SecGovAPI::MOST_RECENT_REPORT) {
        if ($iSearchType !== SecGovAPI::MOST_RECENT_REPORT && $iSearchType !== SecGovAPI::ALL_REPORTS) {
          return array();
        }
        $aReportLinks = $this->search(SecGovAPI::QUARTERLY_REPORT, $iSearchType);
        var_dump($aReportLinks);
        foreach ($aReportLinks as $link) {
          $this->oParallelCurl->addUrl(SecGovAPI::BASE_LINK."/".$link);
        }
        $aData = $this->oParallelCurl->run();
        foreach ($aData as $link => $xml) {
          $this->oDoc->loadXML($xml);
          $oXPath = new DOMXPath($this->oDoc);
          $oXPath->registerNamespace("xbrli", "http://www.xbrl.org/2003/instance");
          $this->getReportData($oXPath, $link, "dei:EntityCommonStockSharesOutstanding", "CommonStockSharesOutstanding");
          $this->getReportData($oXPath, $link, "us-gaap:NetIncomeLoss", "NetIncomeLoss");
       }
        var_dump($this->report);
      }
      public function getYearlyReport($iSearchType = SecGovAPI::MOST_RECENT_REPORT) {
        if ($iSearchType !== SecGovAPI::MOST_RECENT_REPORT && $iSearchType !== SecGovAPI::ALL_REPORTS) {
          return array();
        }
        $aReportLinks = $this->search(SecGovAPI::YEARLY_REPORT, $iSearchType);
        var_dump($aReportLinks);
        foreach ($aReportLinks as $link) {
          $this->oParallelCurl->addUrl(SecGovAPI::BASE_LINK."/".$link);
        }
        $aData = $this->oParallelCurl->run();
        foreach ($aData as $link => $xml) {
          $this->oDoc->loadXML($xml);
          $oXPath = new DOMXPath($this->oDoc);
          $oXPath->registerNamespace("xbrli", "http://www.xbrl.org/2003/instance");
          $this->getReportData($oXPath, $link, "dei:EntityCommonStockSharesOutstanding", "CommonStockSharesOutstanding");
          $this->getReportData($oXPath, $link, "us-gaap:NetIncomeLoss", "NetIncomeLoss");
        }
        var_dump($this->report);
      }
      public function getReportData($oXPath, $link, $sEntity, $sSaveAs) {
        $oNodelist = $oXPath->query("//xbrli:xbrl/".$sEntity);
        for ($i = 0; $i < $oNodelist->length; $i++) {
          $this->report[$link][$sSaveAs][$i] = $oNodelist->item($i)->nodeValue;
        }
      }
      protected function search($sType, $iSearchType) {
        $aParams = array("company" => "",
                         "match" => "",
                         "CIK" => $this->sSymbol,
                         "filenum" => "",
                         "State" => "",
                         "Country" => "",
                         "SIC" => "",
                         "count" => "40",
                         "owner" => "exclude",
                         "Find" => "Find Companies",
                         "action" => "getcompany",
                         "type" => $sType,
                         "output" => "atom");
        $sUrl = SecGovAPI::SEARCH_LINK . "?". http_build_query($aParams);
        $this->oDoc->load($sUrl);
        $oXPath = new DOMXPath($this->oDoc);
        $oXPath->registerNamespace("atom", "http://www.w3.org/2005/Atom");
        $aLinks = $this->getReportLinks($oXPath, $iSearchType);
        $aReportLinks = array();
        foreach ($aLinks as $link) {
          $this->oParallelCurl->addUrl($link);
        }
        $aHtmls = $this->oParallelCurl->run();
        foreach ($aHtmls as $html) {
          $this->oDoc->loadHTML($html);
          $oXPath = new DOMXPath($this->oDoc);
          $oNodeList = $oXPath->query('//table[@summary="Data Files"]/tr[2]/td[3]/a');
          if ($oNodeList->length === 0) continue;
          $aReportLinks[] = $oNodeList->item(0)->getAttribute("href");
        }
        return $aReportLinks;
      }
      protected function getReportLinks($oXPath, $iSearchType) {
        if ($iSearchType === SecGovAPI::MOST_RECENT_REPORT) {
          $aNodeList = $oXPath->query("//atom:feed/atom:entry[1]/atom:link");
          if ($aNodeList->length === 1) 
            return array($aNodeList->item(0)->getAttribute("href"));
          else 
            return null;
        }
        else if ($iSearchType === SecGovAPI::ALL_REPORTS) {
          $aNodeList = $oXPath->query("//atom:feed/atom:entry/atom:link");
          for ($i = 0; $i < $aNodeList->length; $i++) {
            $aReportLinks[] = $aNodeList->item($i)->getAttribute("href");
          }
          return $aReportLinks;
        }
        else return null;
      }
      protected function getAccessionNumber($oXPath, $iSearchType) {
        if ($iSearchType === SecGovAPI::MOST_RECENT_REPORT) {
          $aNodeList = $oXPath->query("//atom:feed/atom:entry[1]/atom:id");
          if ($aNodeList->length === 1) $sRawAccessNumber = $aNodeList->item(0)->nodeValue;
          else return null;
          return $this->processAccessionNumber($sRawAccessNumber);
        }
        else if ($iSearchType === SecGovAPI::ALL_REPORTS) {
          $aNodeList = $oXPath->query("//atom:feed/atom:entry/atom:id");
          for ($i = 0; $i < $aNodeList->length; $i++) {
            $sRawAccessNumber = $aNodeList->item($i)->nodeValue;
            $aAccessionNumber[] = $this->processAccessionNumber($sRawAccessNumber);
          }
          return $aAccessionNumber; 
        }
        else return null;
      }
      protected function processAccessionNumber($sRawAccessNumber) {
        $aSplitData = preg_split('/accession-number=/', $sRawAccessNumber);
        if (!isset($aSplitData[1])) return null;
        $sAccessionNumber = str_replace('-','', $aSplitData[1]);
        return $sAccessionNumber;
      }
      protected function getReportDate($oXPath, $iSearchType) {
        if ($iSearchType === SecGovAPI::MOST_RECENT_REPORT) {
          $aNodeList = $oXPath->query("//atom:feed/atom:entry[1]/atom:updated");
          if ($aNodeList->length === 1) 
            return $sUpdated = $aNodeList->item(0)->nodeValue;
          else 
            return null;
        }
        else if ($iSearchType === SecGovAPI::ALL_REPORTS) {
          $aNodeList = $oXPath->query("//atom:feed/atom:entry/atom:updated");
          for ($i = 0; $i < $aNodeList->length; $i++) {
            $aUpdated[] = $aNodeList->item($i)->nodeValue;
          }
          return $aUpdated; 
        }
        else return null;
      }
    }
    ?>

parallelCurl.php

<?php
class ParallelCurl {
  protected $aHandlers;
  public function __construct() {
    $this->aHandlers = array();
    $this->rMultiHandler = curl_multi_init();
  }
  public function addUrl($sUrl) {
    $rHandler = curl_init();
    curl_setopt($rHandler, CURLOPT_URL, $sUrl);
    curl_setopt($rHandler, CURLOPT_HEADER, 0);
    curl_setopt($rHandler, CURLOPT_RETURNTRANSFER, 1);
    curl_multi_add_handle($this->rMultiHandler, $rHandler);    
    $this->aHandlers[$sUrl] = $rHandler;
  }
  public function run() {
    $blsRunning = null;
    do {
      $rHandler = curl_multi_exec($this->rMultiHandler, $blsRunning);
    } while ($rHandler === CURLM_CALL_MULTI_PERFORM);
    while ($blsRunning && $rHandler == CURLM_OK) {
      if (curl_multi_select($this->rMultiHandler) != -1) {
        do {
          $rHandler = curl_multi_exec($this->rMultiHandler, $blsRunning);
        } while ($rHandler == CURLM_CALL_MULTI_PERFORM);
      }
    }
    foreach ($this->aHandlers as $url => $handler) {
      $data[$url] = curl_multi_getcontent($handler);
      curl_multi_remove_handle($this->rMultiHandler, $handler);
    }

    $this->aHandlers = array();
    return $data;
  }
  public function __destruct() {
    curl_multi_close($this->rMultiHandler);
  }
}
?>

1 个答案:

答案 0 :(得分:1)

要创建SecGovAPI课程的实例,您可以使用new关键字:

$class = new SecGovAPI($sSymbol);

然后您可以使用以下方法调用方法getQuaterlyReportgetYearlyReport

echo $class->getQuaterlyReport(); 

echo $class->getYearlyReport();

这两种方法都有一个参数,默认为SecGovAPI::MOST_RECENT_REPORT。您也可以使用:

  • SecGovAPI::QUARTERLY_REPORT
  • SecGovAPI::YEARLY_REPORT
  • SecGovAPI::ALL_REPORTS

示例:

echo $class->getQuaterlyReport(SecGovAPI::QUARTERLY_REPORT);