3
\$\begingroup\$

What I need to do is fetch the XML link using curl then get a student ID and check if the ID is present in the database. If yes, then I need to get other details from another database table.

My code works fine but it's kinda slow. I later need to add more functions in this which will make the loading speed slower. Is there any way I can improve this code?

It currently takes around 5-6 second to display result when XML has 10-12 ID elements.

<?php

$parseXml = "example.com/xml.xml";
$result = curl_json($parseXml);
if ($result['httpcode'] === 200) {
    $output = simplexml_load_string($result['result']) or die('err');
    if ($output->result) {
        echo 'Results: ';
        foreach ($output->result as $resultItem) {
            echo checkID($resultItem->title, $resultItem->dataID);
        }
        echo '';
    } else {
        echo 'no result';
    }
}

function checkID($title, $id) {
    $con = mysql_connect(HOST, DBUSER, DBPASS);
    if ($con) {
        mysql_select_db(DBNAME);
        $sql = "SELECT * FROM idlist WHERE id='$id'";
        $retval = mysql_query($sql);
        if (!$retval) {
            die();
        }
        $num_rows = mysql_num_rows($retval);
        if ($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
            $out['val'] = 'yes';
            $out['data'] = getInfo($id);
            return $out;
        }
    }
}

function getInfo($id) {
    $con = mysql_connect(HOST, DBUSER, DBPASS);
    if ($con) {
        mysql_select_db(DBNAME);
        $sql = "SELECT * FROM infolist WHERE id='$id'";
        $retval = mysql_query($sql);
        if (!$retval) {
            die();
        }
        if ($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
            $arr = array();
            $arr['name'] = $row['name'];
            $arr['class'] = $row['class'];
            return $arr;
        }
    }
}

function curl_json($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    $result = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    $out['result'] = $result;
    $out['httpcode'] = $httpcode;

    return($out);
}
\$\endgroup\$
  • \$\begingroup\$ You could first try to find out where (in which functions or parts of the functions) expensive (in means of time) calls are done. So, you could measure if maybe the database access takes long or the parsing of the XML structure is to slow. \$\endgroup\$ – mnille Feb 15 '16 at 10:41
  • \$\begingroup\$ Performance is very broad term. There are several hidden things which can play a crucial role. Despite of some specific case, performance related issues are always open ended in all ways. \$\endgroup\$ – Anand Shah Feb 15 '16 at 10:49
  • \$\begingroup\$ You could start by making ONE connection to the database in the main process of the script, that is quite an expensive instruction. Then pass the connection as a param whereever it is needed \$\endgroup\$ – RiggsFolly Feb 15 '16 at 10:52
  • 1
    \$\begingroup\$ Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use \$\endgroup\$ – RiggsFolly Feb 15 '16 at 10:53
  • 2
    \$\begingroup\$ Also note that cURL is dependant upon the speed of the site you are connecting to. There is not much you can do about that if that is the main reason for you execution time. \$\endgroup\$ – RiggsFolly Feb 15 '16 at 10:55
1
\$\begingroup\$
  • mysql_ is deprecated, and it has been for quite a while. It will be removed in PHP 7, so you should not use it anymore.
  • you are open to SQL injection. Never put variables directly into SQL queries, always use prepared statements.
  • if you use prepared statements, you could prepare your queries only once, and then execute them with multiple different values, which might increase your performance.
  • use one connection across your code instead of creating new ones for every query (this can increase your performance).
  • profile your code to find out what actually causes performance problems. There is no need to "improve" code that is not causing problems. My guess would be that the curl request is the bottleneck, but without profiling, it's hard to say.
  • return early to reduce your level of nesting. Eg if (!$cond) return false (or throw an exception).
  • don't just silently fail, as it will be extremely difficult to debug your code this way. You silently fail if the httpcode of your curl request isn't 200, you silently fail if you can't create a database connection, etc.
  • don't die in functions, it makes it impossible for the calling code to recover. Throw an exception instead. If you do die, at the very least add a message stating the reason for it, as it will otherwise be really hard to debug your code.
\$\endgroup\$
  • \$\begingroup\$ HI Thank you for your answer. I am new to php and learning day by day. I will do the mysqli prepare later. But can you share answer for other things you shared? With some reference or edits required please if possible? \$\endgroup\$ – Arjun Feb 16 '16 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.