Page 1 of 1

cURL() not working as expected

Posted: Sun Aug 28, 2016 2:00 am
by me!
he while loop functions as it should. I am passing a large file that PHP is processing and I have added the curl() sections to get XML data from a company (QRZ). The problem is the data is not always coming back complete. it truncates the last name ($lname) and just pulls an error other times on some lookup.

The lookup works if you type in a call sign into a form box, the speed of the loop appears to be the problem.

I am not sure if I can make one connection and keep asking for different data?

Code: Select all

        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>";
}

Re: cURL() not working as expected

Posted: Sun Aug 28, 2016 2:37 am
by requinix
First, I doubt you need to get the "session key" multiple times. Move that out of the loop.

Try without cURL. All you really need is

Code: Select all

$xml = simplexml_load_file("http://xml.qrz.com/...");
If that doesn't work, copy() it to a temporary location so you can manually inspect the contents to make sure it's not QRZ that's causing the problem.

Code: Select all

copy("http://xml.qrz.com/...", "/tmp/qrz.{$callsign}.xml"); // or wherever - PHP must have write access to it
$xml = simplexml_load_file("/tmp/qrz.{$callsign}.xml");
And when it produces an error, what is the error? Ignoring it doesn't help anyone.

Re: cURL() not working as expected

Posted: Sun Aug 28, 2016 3:52 pm
by me!
Thanks requinix,

I did take this out of the loop:

Code: Select all

  // 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;
I also removed all the curl function and went with:

Code: Select all

$xml = simplexml_load_file("http://xml.qrz.com/bin/xml?s=".$session_key.";callsign=".$callsign);
I was still getting truncated names, but no errors like before.
then I noticed it was an error with variable name in the echo statement.
I had:

Code: Select all

echo '<li>'.$callsign.', '.$fname.' '.$lname.' was added.</li>'; // incorrect

echo '<li>'.$callsign.', '.$fname.' '.$name.' was added.</li>'; // this one is good $name NOT $lname