codeigniter
XML object not working in Model but works in Controller in CodeIgniter(CI) [Solution]

XML object not working in Model but works in Controller in CodeIgniter(CI) [Solution]

Being as a CI developer, you might get issue creating XML object from url. If that is your case, well this blog is for you. Today, I am going to show you how you are fix the issue easily.

Before starting let me explain what actually is the problem in detail. I tried to get XML object from URL to fetch RSS feeds. Previously, I used method simplexml_load_string() provided by PHP itself. But suddenly, my code stop working and started showing this error message:

https:// wrapper is disabled in the server configuration by allow_url_fopen=0

I requested hosting guy, but didn’t change the setting for me. They said it is because of security issue. Also, I tried to change php_ini file and set allow_url_fopen = 1. But still I was not able to use simplexml_load_string() method. So, I tried to go for an alternative to fetch rss feed. And I found the code to do what I was looking for. Here is the code.

/*
 *
 XML Loader section
 */
 public static function loadXML($url) {
      ini_set('display_errors', 1);
      ini_set('display_startup_errors', 1);

      error_reporting(E_ALL);

      $ch = curl_init();     
      curl_setopt ($ch, CURLOPT_URL, $url);     
      curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);     
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);     

      $contents = curl_exec($ch);     
      $xml = simplexml_load_string($contents);     

      return $xml; 
 } 

To test, I used this code block within my controller section. And I found out that this code was working. Great…

Problem

Now I copied above code to my Model section to manage my code. After coping, I tried to call the very same code from controller. But this time, it doesn’t work. I was like… WHAT????? But after searching and trying few things I finally found a solution. Here is that code that actually works in model section as well.

   /*
    *
    *   XML Loader section
    */
    public static function loadXML($url) {
        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);

        $ch = curl_init();
        
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

        /*  Add this line of code  */
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 

        $contents = curl_exec($ch);
        
        $xml = simplexml_load_string($contents);

        return $xml;
    }

Solution

What I did is just added curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); line of code. And viola, it worked.

Well, I actually didn’t know in detail why it was not working in model before adding that new line of code. But my plan for this blog was to make sure you find the solution. So, I hope you got the solution and fix your issue. If you know what that new line of code is making it work, please let me know.

Thanks for reading my blog. And I hope this blog was helpful to you. If so, don’t forget to share it.

?Peace?

Leave a Reply