Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - cURL file from localhost not working

Tags:

php

curl

I copied a working script from my PHP server, but for development purposes, I would like it to work from my local XAMPP server.

The cURL:

        $realpath_curl_file = realpath($curl_file);

        $post = array(
                'recipient_number' => $recipient_number,
                'user_id' => $user_id,
                'up_file'=> "@$realpath_curl_file"
        );

        //prepare data for cUrl
        $target_url = "http://api.blankthis.com/curl/outgoing";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $target_url);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        $result = curl_exec ( $ch );
        $err = curl_errno ( $ch );
        $errmsg = curl_error ( $ch );
        $header = curl_getinfo ( $ch );
        $httpCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
        print_r($result);
        echo '------------------------';
        print_r($ch);
        print_r($err);
        print_r($errmsg);
        print_r($header);
        print_r($httpCode);

When I do a print_r($_POST) and print_r($_FILES), no files are being transfered. This is my result:

POST:Array ( [recipient_number] => 2394434455 [user_id] => 2 [up_file] => @C:\Users\Sharktek\AppData\Local\Temp\1422046077466.zip ) 

FILES:Array ( ) 

------------------------
Resource id 
#570
Array ( [url] => http://api.redfax.com/curl/outgoing [content_type] => text/html; charset=UTF-8 [http_code] => 200 [header_size] => 202 [request_size] => 196 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.499 [namelookup_time] => 0.125 [connect_time] => 0.218 [pretransfer_time] => 0.218 [size_upload] => 409 [size_download] => 168 [speed_download] => 336 [speed_upload] => 819 [download_content_length] => 168 [upload_content_length] => 409 [starttransfer_time] => 0.359 [redirect_time] => 0 [redirect_url] => [primary_ip] => 107.191.119.155 [certinfo] => Array ( ) [primary_port] => 80 [local_ip] => 192.168.0.101 [

Does anyone know why my files are not being uploaded via cURL? As I said, this works fine my from my server (non-localhost)

  • XAMPP PHP Installation has cURL enabled
  • I disabled my firewall
like image 645
Nathan_Sharktek Avatar asked Jan 23 '15 20:01

Nathan_Sharktek


People also ask

Does cURL work on localhost?

It also works for localhost, since curl will check the cache before the internal resolve and --resolve populates the DNS cache with the given entries.

What is curl_ setopt in php?

The curl_setopt() function will set options for a CURL session identified by the ch parameter. The option parameter is the option you want to set, and the value is the value of the option given by the option.

Is cURL default in PHP?

cURL is enabled by default but in case you have disabled it, follow the steps to enable it. Open php. ini (it's usually in /etc/ or in php folder on the server). Search for extension=php_curl.

Does cURL use PHP?

Uses of cURL in PHPcURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains.


2 Answers

check you php.ini, your apache conf files, restart, and repeat and repeat and repeat...

or if you're having the same problem as me, and know that curl is loaded, but just not executing external requests, try adding this option to your curl

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

This option determines whether curl verifies the authenticity of the peer's certificate

src

The problem is likely caused by having an out-to-date certificate. Especially if you're developing on Windows and use XAMPP or some comparable service, the certificates are not loaded by default. On Linux this is less likely to be required.

For production use, you should fix the root problem instead of allowing this vulnerability to affect your server communication.

like image 105
Daniel Avatar answered Oct 09 '22 06:10

Daniel


If you're using XAMPP have you checked the php.ini

in the XAMPP installation directory open up %XAMPP_HOME%/php/php.ini file then uncomment the following line extension=php_curl.dll

from

;extension=php_curl.dll

to this

extension=php_curl.dll

if that dll doesn't exist check whether %XAMPP_HOME%/php/ext/php_curl.dll is there if not you can get it online and put it there.

after you're done all that then restart apache

this should be the only hold up on windows with php and cURL

like image 1
unixmiah Avatar answered Oct 09 '22 04:10

unixmiah