curl 504 网关超时?

问题描述 投票:0回答:1

我获取 xml 提要,并且想要获取从 2000 年至今所有联赛的所有足球比赛,但我收到 504 网关错误。

Feed 真的很大,如果我从 2014 年到现在尝试,它会起作用。但如果我从 2000 年到现在尝试,就会出现 504 超时。我尝试过

ini_set('max_execution_time',0);
ini_set('request_terminate_timeout',0);

curl_setopt($curl, CURLOPT_TIMEOUT, self::TIMEOUT_CURL); //0
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, self::TIMEOUT_CURL); //0

但又是504

这是解析器的代码:

class XMLSoccer{
protected $service_url="http://www.xmlsoccer.com/FootballData.asmx";
protected $api_key,$request_ip;
const TIMEOUT_GetLiveScore=25;
const TIMEOUT_GetLiveScoreByLeague=25;
const TIMEOUT_GetOddsByFixtureMatchID=3600;
const TIMEOUT_GetHistoricMatchesByLeagueAndSeason=3600;
const TIMEOUT_GetAllTeams=3600;
const TIMEOUT_GetAllTeamsByLeagueAndSeason=3600;
const TIMEOUT_Others=300;
const TIMEOUT_CURL=0;
public function __construct($api_key=""){
$this->request_ip=gethostbyname(gethostname());
if(empty($this->service_url)) throw new XMLSoccerException("service_url cannot be empty. Please setup");
if(!empty($api_key)) $this->api_key=$api_key;
if(empty($this->api_key)) throw new XMLSoccerException("api_key cannot be empty. Please setup.");
}
/*
list available methods with params.
*/
public function __call($name,$params){
$data=$this->request($this->buildUrl($name,$params));
if(false===($xml = simplexml_load_string($data))) throw new XMLSoccerException("Invalid XML");
if(strstr($xml[0],"To avoid misuse of the service")){
switch($name){
case "GetLiveScore":
case "GetLiveScoreByLeague":
case "GetOddsByFixtureMatchID":
case "GetHistoricMatchesByLeagueAndSeason":
case "GetAllTeams":
case "GetAllTeamsByLeagueAndSeason":
throw new XMLSoccerException($xml[0],constant("self::TIMEOUT_".$name));
default:
throw new XMLSoccerException($xml[0],self::TIMEOUT_Others);
}
}
return $xml;
}
protected function buildUrl($method,$params){
$url=$this->service_url."/".$method."?apikey=".$this->api_key;
for($i=0;$i<count($params);$i++){
if(is_array($params[$i])){
foreach($params[$i] as $key=>$value){
$url.="&".strtolower($key)."=".rawurlencode($value);
}
}
else{
throw new XMLSoccerException("Arguments must be an array");
}
}
return $url;
}
protected function request($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, self::TIMEOUT_CURL);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, self::TIMEOUT_CURL);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//curl_setopt($curl, CURLOPT_INTERFACE,$this->request_ip);
$data = curl_exec($curl);
$cerror=curl_error($curl);
$cerrno=curl_errno($curl);
$http_code=curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if($cerrno!=0) throw new XMLSoccerException($cerror,E_USER_WARNING);
if($http_code == 200 ) return $data;
throw new XMLSoccerException($http_code .' - '. $data . "\nURL: " . $url);
}
public function setRequestIp($ip){
if(empty($ip)) throw new XMLSoccerException("IP parameter cannot be empty",E_USER_WARNING);
$this->request_ip=$ip;
}
public function setServiceUrl($service_url){
if(empty($service_url)) throw new XMLSoccerException("service_url parameter cannot be empty",E_USER_WARNING);
$this->service_url=$service_url;
}
}
class XMLSoccerException extends Exception{
}

这是历史比赛的主要代码。

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

ini_set('max_execution_time',0);
ini_set('request_terminate_timeout',0);

$xml = new XMLtoDB(NULL);
//$xml->getXsScorers();

function sendHistoricToDb(){
    $pro = new Provider();
    $con = new Connect();
    $PDO = $con->PDO;
    $con->connect();
    $PDO->beginTransaction();
        $soccer = new XMLSoccer($pro->api);
    $stmt = $PDO->prepare($pro->insHis);

    $res = $PDO->query("select LeagueID from league where Source='xmlsoccer'");
    $res->setFetchMode(PDO::FETCH_ASSOC);
    while($row = $res->fetch()){
        $xml = $soccer->GetHistoricMatchesByLeagueAndDateInterval(
        array("league"=>$row['LeagueID'],"startDateString"=>"2000-01-01","endDateString"=>date('Y-m-d H:i:s')));
        foreach($xml->Match as $match){
            //var_dump($match);
            $hisid = (int)$match->Id;
            $fixid = (int)$match->FixtureMatch_Id;
            $date = date('Y-m-d H:i:s',strtotime((string)$match->Date));
            $round = (int)$match->Round;
            $spec = (string)$match->Spectators;
            $league = (string)$match->League;
            $hteam = (string)$match->HomeTeam;
            $hteamid = (int)$match->HomeTeam_Id;
            $hcorn = (int)$match->HomeCorners;
            $hgoals = (int)$match->HomeGoals;
            $hthgoals = (int)$match->HalfTimeHomeGoals;
            $hshots = (int)$match->HomeShots;
            $hsont = (int)$match->HomeShotsOnTarget;
            $hfouls = (int)$match->HomeFouls;
            $hgd = (string)$match->HomeGoalDetails;
            $hgk = (string)$match->HomeLineupGoalkeeper;
            $hdf = (string)$match->HomeLineupDefense;
            $hmdf = (string)$match->HomeLineupMidfield;
            $hfw = (string)$match->HomeLineupForward;
            $hls = (string)$match->HomeLineupSubstitutes;
            $hlc = (string)$match->HomeLineupCoach;
            $hycard = (int)$match->HomeYellowCards;
            $hrcard = (int)$match->HomeRedCards;
            $htf = (string)$match->HomeTeamFormation;
            $ateam = (string)$match->AwayTeam;
            $ateamid = (int)$match->AwayTeam_Id;
            $acorn = (int)$match->AwayCorners;
            $agoals = (int)$match->AwayGoals;
            $athgoals = (int)$match->HalfTimeAwayGoals;
            $ashots = (int)$match->AwayShots;
            $asont = (int)$match->AwayShotsOnTarget;
            $afouls = (int)$match->AwayFouls;
            $agd = (string)$match->AwayGoalDetails;
            $agk = (string)$match->AwayLineupGoalkeeper;
            $adf = (string)$match->AwayLineupDefense;
            $amdf = (string)$match->AwayLineupMidfield;
            $afw = (string)$match->AwayLineupForward;
            $aycard = (int)$match->AwayYellowCards;
            $arcard = (int)$match->AwayRedCards;
            $als = (string)$match->AwayLineupSubstitutes;
            $alc = (string)$match->AwayLineupCoach;
            $atf = (string)$match->HomeTeamFormation;

            $hsubd = (string)$match->HomeSubDetails;
            $asubd = (string)$match->AwaySubDetails;
            $htycd = (string)$match->HomeTeamYellowCardDetails;
            $atycd = (string)$match->AwayTeamYellowCardDetails;
            $htrcd = (string)$match->HomeTeamRedCardDetails;
            $atrcd = (string)$match->AwayTeamRedCardDetails; 
            $source = 'xmlsoccer';

            execHistoric($stmt,$hisid,$fixid,$date,$round,$spec,$league,$hteam,$hteamid,$hcorn,$hgoals,$hthgoals,$hshots,$hsont,$hfouls,$hgd,$hgk,$hdf,$hmdf,$hfw,$hls,$hlc,$hycard,$hrcard,$htf,$ateam,$ateamid,$acorn,$agoals,$athgoals,$ashots,$asont,$afouls,$agd,$agk,$adf,$amdf,$afw,$als,$alc,$aycard,$arcard,$atf,$hsubd,$asubd,$htycd,$atycd,$htrcd,$atrcd,$source);
        }
    }
    $PDO->commit();
    $con->close();
}

function execHistoric($stmt,$hisid,$fixid,$date,$round,$spec,$league,$hteam,$hteamid,$hcorn,$hgoals,$hthgoals,$hshots,$hsont,$hfouls,$hgd,$hgk,$hdf,$hmdf,$hfw,$hls,$hlc,$hycard,$hrcard,$htf,$ateam,$ateamid,$acorn,$agoals,$athgoals,$ashots,$asont,$afouls,$agd,$agk,$adf,$amdf,$afw,$als,$alc,$aycard,$arcard,$atf,$hsubd,$asubd,$htycd,$atycd,$htrcd,$atrcd,$source){
    $stmt->bindParam(':hisID', $hisid);
    $stmt->bindParam(':fixID',$fixid);
    $stmt->bindParam(':pdate',$date);
    $stmt->bindParam(':round',$round);
    $stmt->bindParam(':spectators',$spec);
    $stmt->bindParam(':league',$league);
    $stmt->bindParam(':homeTeam',$hteam);
    $stmt->bindParam(':homeTeamID',$hteamid);
    $stmt->bindParam(':homeCorners',$hcorn);
    $stmt->bindParam(':homeGoals',$hgoals);
    $stmt->bindParam(':halfTimeHomeGoals', $hthgoals);
    $stmt->bindParam(':homeShots', $hshots);
    $stmt->bindParam(':homeShotsOnTarget', $hsont);
    $stmt->bindParam(':homeFouls',$hfouls);
    $stmt->bindParam(':homeGoalDetails', $hgd);
    $stmt->bindParam(':homeLineupGoalkeeper',$hgk);
    $stmt->bindParam(':homeLineupDefense',$hdf);
    $stmt->bindParam(':homeLineupMidfield',$hmdf);
    $stmt->bindParam(':homeLineupForward',$hfw);
    $stmt->bindParam(':homeLineupSubstitutes',$hls);
    $stmt->bindParam(':homeLineupCoach',$hlc);
    $stmt->bindParam(':homeYellowCards',$hycard);
    $stmt->bindParam(':homeRedCards',$hrcard);
    $stmt->bindParam(':homeTeamFormation',$htf);
    $stmt->bindParam(':awayTeam',$ateam);
    $stmt->bindParam(':awayTeamID',$ateamid);
    $stmt->bindParam(':awayCorners', $acorn);
    $stmt->bindParam(':awayGoals', $agoals);
    $stmt->bindParam(':halfTimeAwayGoals', $athgoals);
    $stmt->bindParam(':awayShots',$ashots);
    $stmt->bindParam(':awayShotsOnTarget', $asont);
    $stmt->bindParam(':awayFouls',$afouls);
    $stmt->bindParam(':awayGoalDetails',$agd);
    $stmt->bindParam(':awayLineupGoalkeeper',$agk);
    $stmt->bindParam(':awayLineupDefense',$adf);
    $stmt->bindParam(':awayLineupMidfield',$amdf);
    $stmt->bindParam(':awayLineupForward',$afw);
    $stmt->bindParam(':awayLineupSubstitutes',$als);
    $stmt->bindParam(':awayLineupCoach',$alc);
    $stmt->bindParam(':awayYellowCards',$aycard);
    $stmt->bindParam(':awayRedCards',$arcard);
    $stmt->bindParam(':awayTeamFormation',$atf);
    $stmt->bindParam(':homeSubDetails', $hsubd);
    $stmt->bindParam(':awaySubDetails', $asubd);
    $stmt->bindParam(':homeTeamYellowCardDetails', $htycd);
    $stmt->bindParam(':awayTeamYellowCardDetails',$atycd);
    $stmt->bindParam(':homeTeamRedCardDetails', $htrcd);
    $stmt->bindParam(':awayTeamRedCardDetails', $atrcd);
    $stmt->bindParam(':source',$source);
    $stmt->execute();
}
sendHistoricToDb();
?>
php mysql curl simplexml
1个回答
0
投票

根据我的说法: 书面脚本与其他网站元素冲突 大文件会增加加载时间 缓存问题阻碍优化页面加载 插件正在影响您的网站 互联网问题正在损害特定用户的性能

© www.soinside.com 2019 - 2024. All rights reserved.