• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help with CURL

#1
Hello,

I am currently using CodeIgniter 2.2.3 and I am trying to execute a CURL Statement which requires me to use '@' before the FileURL.

The problem is, I am running php 5.5.9 and using '@' before URL has been deprecated after php 5.5.

I tried using $data = array( 'file' => new CURLFile($filePath) );

But this doesn't seem to run correctly when I execute the script. Digging a few more thing, I found out it is necessary to set CURLOPT_SAFE_UPLOAD to true, so I did that as well, but its still not working!

I get no reply from the API Server whatsoever! I asked the API Developer to test my code and he said it worked perfectly fine for him (just running it using plain PHP), but with CodeIgniter, I am not able to run the script correctly.

What am I doing wrong? Is there anything else that has to be done?


Thanks,
Reply

#2
Well... maybe you can start by posting the code snippet that is not working? I'm using CURL in a CodeIgniter project and it's working fine. CI is not the problem here.
Test your translation files with Translation Tester
Reply

#3
Do you have any options, or is cURL all you get? I usually use phpseclib for file transfers to other servers, as its implementation of SFTP makes it super easy to use. I've used cURL a lot, and used it with CodeIgniter a lot. CodeIgniter doesn't do anything to inhibit your cURL usage, so if it's not working it's your code, your server, your firewall, your ISP, etc.
Reply

#4
(12-17-2015, 06:44 PM)includebeer Wrote: Well... maybe you can start by posting the code snippet that is not working? I'm using CURL in a CodeIgniter project and it's working fine. CI is not the problem here.

Thanks for you reply and sry for my late reply... Here is the code....

PHP Code:
//$somefile_info['file'] = name_of_somefile.extension
//$somefile_info['folder_id'] = Saved in db, that is how we get the file.

 
   $new_name_for_api $somefile_info['somefile_name']. ' ['.$somefile_info['quality'].'] ['.$somefile_info['uploader']. ']';
 
   $details = array('name' => $new_name_for_api'category' => 28'apikey' => 'd67276793f5c9158e977cc34104e07c2''somefile' => new CURLFile(site_url('/uploads/somefiles/folder/'.$somefile_info['folder_id'].'/'.$somefile_info['file'])), 'descr' => 'Visit Us http://somesite.com/');

 
   function upload_to_api($details) {
 
       $url "http://site.com/api-upload.php";
 
       $connect_timeout 10;
 
       $data_timeout 30;
 
       
        $ch 
curl_init();
 
       curl_setopt ($chCURLOPT_URL$url);
 
       curl_setopt ($chCURLOPT_USERAGENT"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
 
       curl_setopt ($chCURLOPT_RETURNTRANSFER1);
 
       curl_setopt ($chCURLOPT_CONNECTTIMEOUT$connect_timeout);
 
       curl_setopt ($chCURLOPT_TIMEOUT$data_timeout);
 
       curl_setopt ($chCURLOPT_POST1);
 
       curl_setopt ($chCURLOPT_SAFE_UPLOAD1);
 
       curl_setopt ($chCURLOPT_POSTFIELDS$details);
 
       $result curl_exec ($ch);
 
       $http_code curl_getinfo ($chCURLINFO_HTTP_CODE);
 
       curl_close ($ch); 

 
       if ($http_code != "200") {
 
           return null;
 
       } else {
 
           if (!($json json_decode($result)) || json_last_error()) {                                            
                $error 
'There was a issue decoding the response.';
 
               return $error;
 
           } else if (isset($json->error) && !empty($json->error)) {
 
               $error $json->error;
 
               return $error;
 
           } else {
 
               return $json;
 
           }
 
          
    
}


 
   $uploaded upload_to_api($details);
 
   if(!empty($uploaded->ok)) {
 
       echo '<b>'.$uploaded->ok.'</b>';
 
   } else {
 
       print_r($uploaded);
 
   

Let me know what's wrong with this...

(12-17-2015, 09:19 PM)skunkbad Wrote: Do you have any options, or is cURL all you get? I usually use phpseclib for file transfers to other servers, as its implementation of SFTP makes it super easy to use. I've used cURL a lot, and used it with CodeIgniter a lot. CodeIgniter doesn't do anything to inhibit your cURL usage, so if it's not working it's your code, your server, your firewall, your ISP, etc.

Not really, the website I am trying to upload the file to, only has an API, which I can connect to, only using CURL. It can not actually be my server because, this problem is arising only when I try to use new CURLFile() or curl_file_create() function. I have tried to connect to another site using CURL (which doesn't require '@') before the URL and it works perfectly fine (without CURLFile). The problem is with the API's that allow file upload using '@' before URL, that is, where new CURLFile() is required...

Let me know what I am doing.... Btw, thanks for replying...
Reply

#5
Check for curl error :

PHP Code:
if(curl_exec($ch) === false)
{
 
   echo 'Error : ' curl_error($ch);

Test your translation files with Translation Tester
Reply

#6
(12-20-2015, 07:19 AM)includebeer Wrote: Check for curl error :

PHP Code:
if(curl_exec($ch) === false)
{
 
   echo 'Error : ' curl_error($ch);


Thanks for reply includebeer.

It seems I was doing a very silly mistake here... When I used the curl_error($ch), t first gave me this error :

Code:
Error : couldn't open file "http://somesite.com/uploads/somefiles/folder/1/name_of_somefile.extension"

After digging a little bit more on that, I came to know that CURLFile can only take relative paths as input. And then, when I changed the array to this... It started working!!!

PHP Code:
$details = array('name' => $new_name_for_api'category' => 28'apikey' => 'd67276793f5c9158e977cc34104e07c2''somefile' => new CURLFile('var/someperson/somethingsomething/somesite/uploads/somefiles/folder/'.$somefile_info['folder_id'].'/'.$somefile_info['file'])), 'descr' => 'Visit Us http://somesite.com/'); 


Thanks for all your help! Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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