CodeIgniter Forums
Help With Curl - Please! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Help With Curl - Please! (/showthread.php?tid=21409)



Help With Curl - Please! - El Forum - 08-09-2009

[eluser]Jay Logan[/eluser]
Basically, I can get this to work as a form on an HTML page. All I have to do is hit the POST button and it gets inserted into my 3rd party CRM. The problem is that the "Success Page is just some XML with the details of the insert.

Code:
&lt;?= form_open('http://crm.zoho.com/crm/private/xml/Leads/insertRecords') ?&gt;<br>
&lt;?= form_hidden('apikey', 'MYAPIKEY') ?&gt;<br>
&lt;?= form_hidden('xmlData', $lead_info) ?&gt;<br>
&lt;?= form_hidden('loginName', 'MYUSERNAME') ?&gt;<br>
&lt;?= form_submit('post', 'Post') ?&gt;<br>
&lt;?= form_close() ?&gt;

So I decided to try Curl for the first time and I'm using this code as it seems logically the most similar to my form.

Code:
$values = array('Market' => 'Atlanta', 'First Name' => 'John', 'Last Name' => 'Doe');
$lead_info = $this->array_to_xml('Leads', $values);
$post_fields = array('xmlData' => $lead_info, 'loginName' => 'MYUSERNAME', 'apikey' => 'MYAPIKEY');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_URL, 'http://crm.zoho.com/crm/private/xml/Leads/insertRecords');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$result = curl_exec($ch);
curl_close($ch);
return $result;

But it doesn't insert the data. It returns XML with an error saying API KEY is required. So I change to URL to http://crm.zoho.com/crm/private/xml/Leads/insertRecords?loginName=MYUSERNAME&apikey=MYAPIKEY and now it just gives a generic error in the XML.

My question is do I need to setopt some more details that would make the processing script think the Curl is the same as the HTML form? And is there any way to see what all gets passes to the processing script during a successful post from the HTML form. Thanks.


Help With Curl - Please! - El Forum - 08-09-2009

[eluser]pistolPete[/eluser]
[quote author="J-Slim" date="1249847046"]...
But it doesn't insert the data. It returns XML with an error saying API KEY is required. So I change to URL to http://crm.zoho.com/crm/private/xml/Leads/insertRecords?loginName=MYUSERNAME&apikey=MYAPIKEY and now it just gives a generic error in the XML.[/quote]

The PHP documentation says:
Quote:CURLOPT_POSTFIELDS
The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.
Try this:
Code:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'xmlData='.$lead_info.'&loginName=MYUSERNAME&apikey=MYAPIKEY');

[quote author="J-Slim" date="1249847046"]...
And is there any way to see what all gets passes to the processing script during a successful post from the HTML form.[/quote]

There is a Firefox plugin called "Live HTTP Headers": https://addons.mozilla.org/en-US/firefox/addon/3829
You could also use a generic network sniffer like Wireshark: http://www.wireshark.org/


Help With Curl - Please! - El Forum - 08-09-2009

[eluser]Jay Logan[/eluser]
Thanks Pistol Pete. I have it working now. I created a library and made this function to get it working.

Code:
public function insert_lead($values)
    {
        $lead_info = $this->array_to_xml('Leads', $values);
    
        $post_data = array('xmlData' => $lead_info, 'loginName' => $this->log_in_name, 'apikey' => $this->api_key);
    
        $post_fields = array();
    
        foreach ($post_data as $key => &$value) {
    
            if (is_array($value)) {
    
                $value = implode(',', $value);
    
            }
    
            $post_fields[] = $key.'='.urlencode($value);
    
        }
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL, $this->api_url.'/Leads/insertRecords');
        curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $post_fields));
        $response = curl_exec($ch);
        curl_close($ch);

    }

That works perfectly. Now I just need a way to parse the XML response into a PHP array. The response looks something like this.

Code:
&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
<response uri="/crm/private/xml/Leads/insertRecords">
<result>
<message>Record(s) added successfully</message>
<recorddetail>
<fieldlabel value="Id">114984000000237085</fieldlabel>
<fieldlabel value="Created Time">2009-08-09 13:10:17</fieldlabel>
<fieldlabel value="Modified Time">2009-08-09 13:10:17</fieldlabel>
<fieldlabel value="Created By">Administrator</fieldlabel>
<fieldlabel value="Modified By">Administrator</fieldlabel>
</recorddetail>
</result>
</response>

Thanks for all the help.


Help With Curl - Please! - El Forum - 08-09-2009

[eluser]pistolPete[/eluser]
Do you use PHP5?
Then have a look at SimpleXML: http://php.net/manual/en/simplexml.examples-basic.php


Help With Curl - Please! - El Forum - 08-09-2009

[eluser]Jay Logan[/eluser]
That will work. Thanks Pete.