Support the ongoing development of Laravel.io →
posted 8 years ago
Requests

Hi,

I'm using a controller to talk to an API which I made. The API requests were working fine until I installed SSL. I can still get results back using Postman for instance but when I change the http to https in my Curl request I get the "Trying to get property of non-object" error. The cert itself seems to be in order and changing the https back to http again works fine. Would like to get peoples' thoughts on this. Basically I pass makeApiRequest('/myMethod', '/myParams') and get JSON results back.

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;


    class myApiController extends Controller
    {
        protected function makeApiRequest($method, $params) {
        $url = "https://www.mysite.com/api" . $method . $params; // http works, https not
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        $output = json_decode($output);
        $statusCode = $output->statusCode; // <- Error points to this line 
        $statusMessage = $output->statusMessage;
        $recordCount = $output->numRecs;
        $data = $output->data;
        return $output;
    }
...
}

Just to reiterate

  • The function worked perfectly well before I changed over to https
  • The function still works perfectly well when I change https back to http

I would appreciate any help.

Last updated 2 years ago.
0

Ok, so found a couple of things for anyone else having this problem. Tried using CURLOPT_SSL_VERIFYPEER set to false, just to try to force the request - it's not a good idea to use in production as it ignores verification of the cert. However, this was still not working. var_dump on the request shows FALSE. By echoing out the curl_error() function, I got the following message which will help lead to the solution - it gives

"SSL: no alternative certificate subject name matches target host name 'www.myapi.com'"

So I'm following this route now - may be that I need to provide Laravel/PHP Curl with a link to a local copy of my cert... I'll post anything new I find.

0

So, for the moment leaving peer and host checking set to false allows me to get my data. I need to investigate why my SSL cert is giving conflicting information about its identity, but for the moment, this will work:

$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // <-- This turns off host verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // <-- And this turns off ssl verification
$output = curl_exec($ch);
return $output;

I'll post a fuller explanation when I've found one :-)

Wittner

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Wittner wittner Joined 12 Jan 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.