Block help: Using cURL in blocks

Permalink
I'm trying to create a block that places a form in the page. When submitting this form, I would like to send the data to an external page where the processing happens and sends back a confirmation code.

I would like to do this using cURL so that the data that is sent remains invisible. This is the first time I am trying to create a block. How should I go about doing this? Do I need to enable cURL somewhere in C5 settings?

BlueFractals
 
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Yes, I'd use CURL. It is not something that needs to be enabled in C5, but it does need to be included in your server's PHP installation. Almost every server/host I've ever used *does* have this set up already, so it's a fairly safe thing to require (for example, the C5 ecommerce module and almost every payment gateway addon assumes that CURL exists).

Here is some sample CURL code:
$url = 'http://example.com';
$post = array(
   'somedata1' => 'your value',
   'somedate2' => 'another value',
   'moredata' => 'yet another value',
);
$request = curl_init($url);
curl_setopt($request, CURLOPT_HEADER, 0);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_POSTFIELDS, $post);
//curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response. [I think this is specific to IIS/Windows servers??]
$response = curl_exec($request);
curl_close($request);
print_r($response);


This of course assumes you already know how to make the general block and know where to put this code. If you aren't sure about that either, though, let me know and I can try to explain that part as well.
BlueFractals replied on at Permalink Reply
BlueFractals
Hi Jordan

Thanks for that. Yes if you could explain where that goes, that will be awesome. I am just starting out with this, so I am not too familiar with block development at this stage. Your explanation could help other people in the future too.

Also, a reply below yours has suggested to use getContents function. But isn't that only for getting the content from the remote url? My concern is to pass data to the remote page.

Thanks
jordanlev replied on at Permalink Reply
jordanlev
Yep. See this post, where I have some sample code for a basic form block:
http://www.concrete5.org/community/forums/customizing_c5/external-f...

As per step 5 in the instructions, put the code you want to run when the form is submitted (your cURL code in this case) into the "action_submit_form" function of the controller.php file.

By the way, I'm assuming you're talking about a form that appears on the front-end of the site for public viewers to fill out -- *not* the "edit" form that an admin user who is managing the site content would see when they edit the page. Let me know if this assumption is incorrect.

HTH

-Jordan
BlueFractals replied on at Permalink Reply
BlueFractals
Thanks Jordan for your help.
Your replies have been very helpful.
12345j replied on at Permalink Reply
12345j
Actually you should use the concrete5 file helpers getContents method.
It has 2 params, the url and the timeout for the operation. So do
$file=loader::helper('file');
$contents=$file->getContents('http://www.url.com');// timout is optional

Basically the method is checks to see if curl is enabled, and then on the .1% of setups that don't have curl, uses file get contents.
If you aren't using c5 though then use curl.
jordanlev replied on at Permalink Reply
jordanlev
Wow, never knew about that one -- thanks!
sethmartin replied on at Permalink Reply
sethmartin
How does this work? I tried to put it into a controller PHP file and go the following error:

Class 'Application\Controller\SinglePage\Loader' not found
VPenkov replied on at Permalink Reply
VPenkov
Try:
$fh = \Core::make("helper/file");
$fh->getContents('YOUR URL HERE');


Also, if you're doing this on a single page, you'd want to create a controller for it and do a
Use File;


Which single page are you trying to modify?
sethmartin replied on at Permalink Reply
sethmartin
Got it!
This worked :)