Rolling-curl Codeigniter library - codeigniter

I've been using this rolling-curl library with success outside of Codeigniter http://code.google.com/p/rolling-curl/source/browse/#svn%2Ftrunk
I want to use the library with Codeigniter but cannot get it to work.
I've created a file called Rollingcurl.php and placed in codeigniter's application/library folder. In this file I've copied the originals RollingCurl.php file's content (see RollingCurl.php file in link posted above).
My controller looks like this:
class Rolling_curl extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index(){
$this->load->library('rollingcurl', 'request_callback');
$this->rollingcurl->request("http://www.google.com");
$this->rollingcurl->execute();
}
function request_callback($response, $info, $request) {
// parse the page title out of the returned HTML
if (preg_match("~<title>(.*?)</title>~i", $response, $out)) {
$title = $out[1];
}
echo "<b>$title</b><br />";
print_r($info);
print_r($request);
echo "<hr>";
}
In the index function I'm loading the rollingcurl library I've created from the orginal RollingCurl.php and passing the parameter 'request_callback', which is the name of the other function in the controller (see orginal rolling-curl example: http://code.google.com/p/rolling-curl/source/browse/trunk/example.php).
But, it doesn't work... What am I doing wrong?

Related

how to save pdf inside controller and pass pdf patch

pdf api -> https://github.com/barryvdh/laravel-dompdf
I create some simple API, everyone who pass some data will recaive link to pdf file.
The thing is, i dont know how to save pdf after conroller make changes inside my blade template.
so...
i already try changing data by request and that was working.
something like :
class pdfGenerator extends Controller
{
public function show(Request $request)
{
$data = $request->json()->all();
return view('test2', compact('data'));
}
}
that work well, also pdf creator works well from web.php
Route::get('/test', function () {
$pdf = PDF::loadView('test2');
return $pdf->download('test2.pdf');
});
that one just download my pdf file as expected.
but, now i try to pust some changes from request and save file but... without efford... any idea?
My code after tray to save content
class pdfGenerator extends Controller
{
public function show(Request $request)
{
$data = $request->json()->all();
return $pdf = PDF::loadView('test2', compact('data'))->save('/pdf_saved/my_test_file.pdf');
}
}
But i get only some errors. Please help :D
How can I save a PDF inside my controller and pass the PDF patch?
$data = $request->json()->all();
$pdf = app('dompdf.wrapper');
$pdf->loadView('test2', $data);
return $pdf->download('test2.pdf');

Codeigniter extend Blueimp Upload library

I have blueimp file upload working well with codeigniter. I use the UploadHandler library as is. But I want to extend it to replace two functions that create the unique filename. The code for this is in the BlueImp Github wiki.
I created an extended library thus:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_UploadHandler extends UploadHandler {
public function __construct() {
parent::__construct();
$CI =& get_instance();
$CI->load->library('UploadHandler');
}
protected function upcount_name_callback($matches) {
$index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
$ext = isset($matches[2]) ? $matches[2] : '';
return '-'.$index.''.$ext;
}
protected function upcount_name($name) {
return preg_replace_callback(
'/(?:(?:-([\d]+))?(\.[^.]+))?$/',
array($this, 'upcount_name_callback'),
$name,
1
);
}
}
When I try to run it, I get an 'unable to load UploadHandler' error. If I remove my MY extension, the original code runs. What is wrong with my extension code? Isn't this the proper way to extend CI libraries?
And, yes, the filename for my file is MY_UploadHandler.php
Thanks!
I noodled over this for a while and finally figured it out. To extend a custom library, you need to 'require once' the file it is extending, prior to the definition of the class.
EX:
require_once("UploadHandler.php");
class MY_UploadHandler extends UploadHandler
{
}
Hopefully that helps.

How to call multiple methods by URI segments in CodeIgniter

I have an issue with CodeIgniter; I have a controller named site, and in this controller there are two methods: production and story.
production calls a specific production via a model which creates production/slug.
What I want to achieve is to create the following URL:
site/production/slug/story
How do I achieve that? As the slug changes, in the story function I want to call a story from the database using $this->uri->segment(3).
You can post multi parameters:
URI: site/production/slug/story/5
public function production($one, $two, $there)
{
echo $one."<br />";
echo $two."<br />";
echo $there."<br />";
}
# OUTPUT
slug
story
5
Pass the method name as second parameter to the first method.
For example, if the URI is site/production/slug/story, pass story to the production method and do necessary checks as below:
class Site extends CI_Controller {
public function __construct()
{
parent::__construct()
}
public function story($text) {
echo $text;
}
public function production($slug = '', $callback = NULL)
{
// Do something with $slug
if (isset($callback) && method_exists(__CLASS__, $callback)) {
$this->{$callback}($slug);
}
}
}
PHPFiddle Demo

call my own library within a view in codeigniter

i just created my own library on this folder (application/library) and following all steps to create individual library,
once i load this library in my controller it execute the function, but when trying to pass it to the view, nothing return
here is my code
MY OWN FUNCTION
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Common {
public function date_arabic()
{
$daysarabic=array('الأحد','الاثنين','الثلاثاء'
,'الأربعاء','الخميس','الجمعة','السبت');
$monarabic=array('','يناير','فبراير','مارس',
'أبريل','مايو','يونيو','يوليو'
,'أغسطس','سبتمبر','أكتوبر','نوفمبر','ديسمبر');
$date=getdate(time());
echo $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;
}
}
MY Controller
//arabic date
$this->load->library('Common');
$this->common->date_arabic();
here it prints out the data in my own function, i tried to store this in a $data to pass it to the view like that
//arabic date
$this->load->library('Common');
$data['date_arabic'] = $this->common->date_arabic();
...
$this->load->view('home_page.php', $data);
then when going to view i just type
<?php echo $date_arabic ; ?>
but nothing returned
In your function, change the last line from this:
echo $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;
to this:
return $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;
when you are writing libraries, you have to manually grab the Codeigniter instance like this
$CI =& get_instance();
then you would use $CI where you would normally use $this to interact with loaded codeigniter resources
so...
instead of
$this->input->post();
you would write
$CI->input->post();
EXAMPLE LIBRARY STRUCTURE
class Examplelib {
// declare your CI instance class-wide private
private $CI;
public function __construct()
{
// get the CI instance and store it class wide
$this->CI =& get_instance();
}
public function lib_function()
{
// use it here
$this->CI->db->etc()
}
public function another_func()
{
// and here
$this->CI->input->post();
}
}

How to change the value of a variable inside a controller from a view file (html)?

I followed this tutorial: http://codeigniter.com/wiki/Internationalization_and_the_Template_Parser_Class/
The controller that loads the language is this one:
<?php
class Example extends Controller {
function Example() {
parent::Controller();
# Load libraries
$this->load->library('parser');
# Load language
$this->lang->load('example', 'english');
}
function index() {
# Load variables into the template parser
$data = $this->lang->language;
# Display view
$this->parser->parse('example', $data);
}
}
?>
In order to change the language I have to manually change english to say spanish in the controller.
What's the best way the user can do this from the index.php file (view)?
The best thing to do is have the user select a supported language on some page, set it as a session variable and call it when ever you need to load a language
$language = $this->session->userdata("language");
$this->lang->load("example", $language);
$data = $this->lang->language;
$this->parser->parse("example", $data);
EDITED BELOW
If you're using CodeIgniter and you're new to this, I wouldn't suggest messing with the index.php file.
You want to do it inside your controller by loading a form where they can pick their language and storing it in the session. I'd also suggest autoloading your session library.
The controller:
<?php
class Home extends Controller {
function Home()
{
parent::Controller();
$this->load->library("session");
}
function index()
{
$language = $this->session->userdata("language");
$this->lang->load("example", $language);
$data = $this->lang->language;
$this->parser->parse("example", $data);
}
function set_lang()
{
if( ! $this->form_validation->run())
{
$this->load->view("select_language_form");
}
else
{
$language = $this->input->post('language', TRUE);
$this->session->set_userdata('language', $language);
redirect('home' 'location');
}
}
}

Resources