Friday, February 27, 2009

cURL problems

Hey guys.  So here is my dilemma.  I am using cURL to get xml data from a website for validation reasons.  As soon as they hit the submit button on my form, it runs the function isValidThing().  My problem is that when I invoke the isValidThing in my controller...it ALWAYS returns false even if I know it should return true.  Here is the code:

Model Code:
<?php
uses('Xml');
    /**
     * Valid Checker: Is Valid Thing
     *
     */

class Thing extends AppModel
{
    var $name = 'Thing';
    var $useTable = 'things';

    var $validate = array(
        'thing' => array(
            'rule' => array('isValidThing'),
            'message' => 'The credentials you supplied are not valid! Please try again'
        )
    );
    function isValidThing()
    {
    $url = "http://www.website.com/sheet.xml?r=".$this->data[$this->name]['value1']."&n=".$this->data[$this->name]['value2'];
    $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
        $curl_thing = curl_init();
        curl_setopt($curl_thing, CURLOPT_URL, $url);
        curl_setopt($curl_thing, CURLOPT_HEADER, 0);
        curl_setopt($curl_thing, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl_thing, CURLOPT_USERAGENT, $agent);
        $curl_result = curl_exec($curl_thing);
        curl_close($curl_thing);

        $responseCharacter = new Xml();
        $responseCharacter->load($curl_result);
        $valid = set::check($responseCharacter, 'thingInfo.thing');
        return $valid;

    }
}
?>

Controller Code:
<?php
class ThingsController extends AppController {

    var $name = 'Things';
    var $uses = array('Thing');
    var $helpers = array('Form', 'Html');
    var $scaffold;
   
    function add() {
        if (!empty($this->data)) {
            $this->Thing->set($this->data);
            if ($this->Thing->isValidCharacter() == true) {
                $this->Session->setFlash($this->data['Thing']['forminput'].' Added', 'default', array('class'=>'good'));
            } else {
                $this->Session->setFlash('Please correct the errors below', 'default', array('class'=>'bad'));
            }
        }
    }
}
?>

My thoughts are either I am not able to make an XML object or the CURL is not connecting to the website.  Any advice?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: