cURL()未按预期工作

时间:2016-08-27 07:49:15

标签: php

while循环可以正常运行。我正在传递PHP正在处理的大文件,我已经添加了curl()部分以从公司(QRZ)获取XML数据。问题是数据并不总是回归完整。它截断了姓氏($ lname),并且在某些查找时只会引发错误。

如果您在表单框中键入呼号,则查找有效,循环的速度似乎是问题。

我不确定我是否可以建立一个连接并继续询问不同的数据?

        while (!feof($fichierADIF))
            {
                $test->setData(fgets($fichierADIF));

                // set vars:
                $raw_qsodate = $test->convert('QSO_DATE');
                $callsign = $test->convert('CALL');


                if ($raw_qsodate != '--'){ // this will filter out non records

                // QRZ lookup

    // Retrieve session key from QRZ
    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, "http://xml.qrz.com/bin/xml?username=********;password=**********;agent=**********");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $retrieved_xml = curl_exec ($curl);
    curl_close ($curl);
    $xml = simplexml_load_string($retrieved_xml);
    // var_dump($xml);
    $session_key = $xml->Session[0]->Key;

    $curl = curl_init();
    // Retrieve QRZ page for specified callsign
    curl_setopt ($curl, CURLOPT_URL, "http://xml.qrz.com/bin/xml?s=".$session_key.";callsign=".$callsign);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $retrieved_xml = curl_exec ($curl);
    curl_close ($curl);
    $xml = simplexml_load_string($retrieved_xml);

    // look to see if call was not found
 if (!($xml->Session[0]->Error)) {
            if ($xml->Callsign[0]->call) {
                    if ($xml->Callsign[0]->fname)    {$fname = $xml->Callsign[0]->fname;}       else {$fname = NULL;}
                    if ($xml->Callsign[0]->name)    {$name = $xml->Callsign[0]->name;}          else {$name = NULL;}
                    if ($xml->Callsign[0]->addr1)   {$addr1 = $xml->Callsign[0]->addr1;}        else {$addr1 = NULL;}
                    if ($xml->Callsign[0]->addr2)   {$addr2 = $xml->Callsign[0]->addr2;}        else {$addr2 = NULL;}
                    if ($xml->Callsign[0]->state)   {$stste = $xml->Callsign[0]->state;}        else {$state = NULL;}
                    if ($xml->Callsign[0]->zip)     {$zip = $xml->Callsign[0]->zip;}            else {$aip = NULL;}
                    if ($xml->Callsign[0]->country) {$country = $xml->Callsign[0]->country;}    else {$country = NULL;}
                    if ($xml->Callsign[0]->codes)   {$codes = $xml->Callsign[0]->codes;}        else {$codes = NULL;}
                    if ($xml->Callsign[0]->efdate)  {$efdate = $xml->Callsign[0]->efdate;}      else {$efdate = NULL;}
                    if ($xml->Callsign[0]->expdate) {$expdate = $xml->Callsign[0]->expdate;}    else {$expdate = NULL;}
                    if ($xml->Callsign[0]->trustee) {$trustee = $xml->Callsign[0]->trustee;}    else {$trustee = NULL;}
                    if ($xml->Callsign[0]->email)   {$email = $xml->Callsign[0]->email;}        else {$email = NULL;}
                    if ($xml->Callsign[0]->url)     {$url = $xml->Callsign[0]->url;}            else {$url = NULL;}
                    if ($xml->Callsign[0]->grid)    {$grid = $xml->Callsign[0]->grid;}          else {$grid = NULL;}
                    if ($xml->Callsign[0]->lat)     {$lat = $xml->Callsign[0]->lat;}            else {$lat = NULL;}
                    if ($xml->Callsign[0]->lon)     {$lon = $xml->Callsign[0]->lon;}            else {$lon = NULL;}
                    if ($xml->Callsign[0]->qslmgr)  {$qslmgr = $xml->Callsign[0]->qslmgr;}      else {$qslmgr = NULL;}
                    if ($xml->Callsign[0]->born)    {$born = $xml->Callsign[0]->born;}          else {$born = NULL;}
                    if ($xml->Callsign[0]->p_call)  {$p_call = $xml->Callsign[0]->p_call;}      else {$p_call = NULL;}
                    if ($xml->Callsign[0]->AreaCode){$areacode = $xml->Callsign[0]->AreaCode;}  else {$areacode = NULL;}
                    if ($xml->Callsign[0]->TimeZone){$timezone = $xml->Callsign[0]->TimeZone;}  else {$timezone = NULL;}
                    if ($xml->Callsign[0]->GMTOffset){$gmtoffset = $xml->Callsign[0]->GMTOffset;}else {$gmtoffset = NULL;}
                    if ($xml->Callsign[0]->DST)     {$dst = $xml->Callsign[0]->DST;}            else {$dst = NULL;}

                    switch ($xml->Callsign[0]->class) {
                                            case "T":
                                                    $class = "Technician";
                                                    break;
                                            case "G":
                                                    $class = "General";
                                                    break;
                                            case "E":
                                                    $class = "Extra";
                                                    break;
                                            case "C":
                                                    $class = "Club";
                                                    break;
                                            case "A":
                                                    $class = "Advanced";
                                                    break;
                                            case "N":
                                                    $class = "Novice";
                                                    break;
                                            case "P":
                                                    $class = "Technician Plus";
                                                    break;
                                            default:
                                                    $class =  $xml->Callsign[0]->class;
                                                    break;
                                    }
            }
echo '<li>'.$callsign.', '.$fname.' '.$lname.' was added.</li>';
}else{  // Callsign NOT found
echo "<li>Callsign ".$callsign." was NOT found!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</li>";
}

1 个答案:

答案 0 :(得分:0)

我怀疑这是卷曲问题。但是,如果您想确保这一点,那么我建议在cURL中添加错误处理。

if(curl_error($curl))
{
    echo 'error:' . curl_error($curl);
}