• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cURL is not working with codeignier 3

#1
In CodeIgniter 3.1.8.  I used cURL.

Code:
public function sample()
{
   $ch = curl_init();
   $target_url = 'https://apiurl.com';
   curl_setopt($ch, CURLOPT_URL,$target_url);
   curl_setopt($ch, CURLOPT_POST,1);
   curl_setopt($ch, CURLOPT_ENCODING, '');
   $args['file'] = new CurlFile($_SERVER['DOCUMENT_ROOT'].'/file/'.'sample.docx');
   curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
   curl_setopt($ch, CURLOPT_HEADER, false);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

   $response = curl_exec($ch);
   if ($response === false) {  
      print_r('Curl error: ' . curl_error($ch));
      die;
   }

   print_r($response);
   die('--------------here-----------------');

}

I can not get any error or response from that.
but the same code is working outside of Codeigniter.
should I use an external library for that? or Should I make some changes to the code?
I checked this one https://github.com/philsturgeon/codeigniter-curl but it is DEPRECATED.
Reply

#2
Try error_reporting(E_ALL) - I suspect errors are disabled, pretty sure $args['file'] = ... should display a warning, as $args is not defined as array before setting a value to it.

What PHP version are you targeting?
Reply

#3
(07-25-2018, 12:59 AM)Pertti Wrote: Try error_reporting(E_ALL) - I suspect errors are disabled, pretty sure $args['file'] = ... should display a warning, as $args is not defined as array before setting a value to it.

What PHP version are you targeting?

Thanks for you reply pertti,

I am using PHP 7.1.
and yes all errors are enabled on my portal I already checked error_reporting(E_ALL). but the same code is run outside of CodeIgniter. Huh
Reply

#4
Did I understood it right, it never gets to the die('--------------here-----------------'); ?

If not, add echo '1'; ... echo '2'; every few lines of code, and you should see at what point does it break.
Reply

#5
The curl_init method returns a false if there is an error.

PHP Code:
public function sample()
{
 
   $ch curl_init();

 
   if ($ch !== false)
 
   {
 
       $target_url 'https://apiurl.com';
 
       curl_setopt($chCURLOPT_URL,$target_url);
 
       curl_setopt($chCURLOPT_POST,1);
 
       curl_setopt($chCURLOPT_ENCODING'');
 
       $args['file'] = new CurlFile($_SERVER['DOCUMENT_ROOT'].'/file/'.'sample.docx');
 
       curl_setopt($chCURLOPT_POSTFIELDS$args);
 
       curl_setopt($chCURLOPT_RETURNTRANSFERTRUE); 
 
       curl_setopt($chCURLOPT_HEADERfalse);
 
       curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
 
       curl_setopt($chCURLOPT_FOLLOWLOCATION1);

 
       $response curl_exec($ch);
 
  
        if 
($response === false)
 
        
            print_r
('Curl error: ' curl_error($ch));
 
           die;
 
       }

 
       print_r($response);
 
       die('--------------here-----------------');
 
   }
 
   else
    
{
 
       // ERROR
 
   }


Give that a try.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
Use below code

public function sample()
{

$ch = curl_init();
$target_url = 'https://apiurl.com';
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_ENCODING, '');
$args['file'] = new CurlFile($_SERVER['DOCUMENT_ROOT'].'/file/sample.docx');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if($httpcode == 200 || $httpcode == '200')
{
print_r($response);
}
else
{
echo "httpcode : ".$httpcode;
if ($response === false)
{
print_r('<br />cURL error: ' . curl_error($ch));

$error_message = curl_strerror($errno);
echo "<br />cURL error ({$errno}):\n {$error_message}";
}
}

}

If its return httpcode than after follow below link for verify exact error :
1) https://developer.mozilla.org/en-US/docs...TTP/Status
2) http://php.net/manual/en/function.http-r...e-code.php

So you can move further easily.
Reply

#7
In case someone needs it this is a CodeIgniter 3 CURL Library.

CodeIgniter : PHP Curl library with SSL Example
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#8
Simple to pull in and use Guzzle HTTP Client for your needs. It's not just for CodeIgniter, so it works everywhere.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.