Home > Enterprise >  WAMP : PHP Curl "working" but returning empty string, file_get_contents is working
WAMP : PHP Curl "working" but returning empty string, file_get_contents is working

Time:01-17

I am running PHP Wampserver 3.2.6 under Windows 11 with Avast Antirus Free edition.

And PHP Version 8.1.0.

Now I have setup a simple curl script to fetch data from a remote host. But this returns nothing. When I put the entire thing online on a server it works just fine. But from a local machine it doesn't work.

I have tried running the entire thing under postman. And there it works just fine.

private function __curl($url, $decode = true){
    // * create curl resource
    $ch = curl_init();
    // * set url
    curl_setopt($ch, CURLOPT_URL, $this->api_url.$url);

    // * return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // * set headers
    $headers = array('X-IM-API-KEY: '.$this->api_key);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, true);


    // * if any postdata set
    if(!empty($this->postdata)){
        // * initialize post
        $this->__post();

        // * set postdata
        curl_setopt($ch,    CURLOPT_POST,       count($this->postdata));
        curl_setopt($ch,    CURLOPT_POSTFIELDS, $this->postfieldstr);
    }


    // $output contains the output string
    $output = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);

    // * if no decode
    if(!$decode) return $output;

    // * return result
    return json_decode($output, true);
    }

When I just use file_get_contents it works fine.

file_get_contents($this->api_url.$url);

The result :

{"success":false,"error":true,"message":"fields missing","data":{"email":"not set","password":"not set","app_version":"1.1"}}

Of course it will give an error because it expects POST parameters with the username and password.

I have the following configuration visible under PHPinfo :

PHP Info CURL configuration

I hope someone can tell me what my mistake would be.

EDIT

When I add :

curl_error($ch);

I get the following error :

SSL certificate problem: unable to get local issuer certificate

But when viewing the address in FireFox I get no error at all. (letscrypt)

enter image description here

EDIT : Answer added by : @codenathan


Adding the following code to disable host and peer verification does the trick actually.

I think in combination with the local firewall the letscrypt certificate simply didn't get through in the way it was supposed to.

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 

Since I need this for developement purposes this actually does the trick for me.

CodePudding user response:

curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);

However it is not ideal to switch this off : https://www.saotn.org/dont-turn-off-curlopt_ssl_verifypeer-fix-php-configuration/

  • Related