Hello everyone,

I'm trying to write a script which accepts URL by GET and then validates the URL before processing it.

for the sake of arguement lets say that the URL I'm expecting is in this format

http://mysite.com/myscript.php?url=http://blahblah.blahblah.bla/blahblah/blahblah/blahblah.bla

http://subdomain.domain.tld/section/category/file.ext

I need to validate the part which is a link to a XML; i.e, my URL might be like these

http://gradefive.belmontprimary.edu/grades/science/peter.xml
https://gradesix.belmontprimary.edu/grades/maths/geometry/lisa.xml
http://gradeeight.belmontprimary.edu/grades/maths/calculus/sandra.xml

I don't know wether if the XML files would be on a secure server (https) or unsecure one (non-https) at the moment, and also I don't know what the contents of the files may be. All I know for sure is that these are the XML files which I need to use for my project.

so far, I have snooped around and found this code

    function isValidURL($link)
    {
        $urlregex = '^(https?|s?ftp\:\/\/)|(mailto\:)';
        $urlregex .= "[a-z0-9+$_-]+(.[a-z0-9+$_-]+)+";
        $urlregex .= '(\?[a-z\+&\$_\.\-][a-z0-9\;\:@\/&%=\+\$_\.\-]*)?';


        if(preg_match('/'.$urlregex.'/i', $link))
        {
            return true;
        }
        else
        {
            return false;
        }
    }


        function getXMLData($url){
        if(!isValidURL($url))   {
            echo "Please enter valid URL including http://<br>";
        }
        else{

            try{
                $savedXML = file_get_contents($url);
                echo "thanks";
                return $savedXML;
            }catch(Exception $e){
                die($e.getMessage());
            }           
        }
    }

but this is not what I exactly need. can anyone please help me by solving this issue?

thanks in advance.

You can write an elaborate regex to test most cases. You could just try to open the file. If it fails... no xml, or invalid url.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.