Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amielkedev's avatar

cURL Requests Not Working When Pushed to Live Server

I'm currently working on a site to showcase knitting patterns my friend has designed, and part of this site uses cURL reqests to pull down a couple json files from Ravelry's site to display those patterns. When working on it locally, everything works fine without issues or errors, but when I pushed it to a sever this evening, there seems to be problems.

I am running the site on a Vultr server running Ubuntu version 16.04 with PHP version 7.0 and Laravel 5.5. I followed the tutorial from DevMarketer on YouTube to set everything up. I also made sure to install curl and php-curl on the server. Everything is working fine on the live version of the site minus the pattern pages that use json files from the cURL requests.

Below is the part of my controller that makes the cURL requests:

protected function getPatternsList()
{
        $login = env('RAVELRY_API_ACCESS_KEY');
        $password = env('RAVELRY_API_PERSONAL_KEY');
        $url = 'https://api.ravelry.com/patterns/search.json?designer=Handiworks+LTD';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
        $result = json_decode(curl_exec($ch));
        $patterns = array_reverse($result->patterns);
        curl_close($ch);
        return $patterns;
}

When I dd $patterns or $results I just end up with null. When I checked the error logs I just got: production.ERROR: Trying to get property of non-object {"exception":"[object] (ErrorException(code: 0): Trying to get property of non-object at /var/www/laravel/app/Http/Controllers/PatternsController.php:22). Line 22 in that file is the line starting with $patterns from above.

I am completely at a loss as to what the issue is, especially since it worked perfectly in a local environment. Any input or suggestions would be greatly appreciated. If any more information about the problem is required to have a better understanding of what is going on, I would be happy to provide it.

0 likes
8 replies
Thyrosis's avatar

Two things come to mind: There could be a restriction on their API based on an IP address, so that when you access the API from a different server it won't let you connect. Your Ravelry dashboard or admin page could help you out on that one.

The second thing is to dump result without json_decode wrapped around the curl_exec. Because now your dumping the result of the json decoded result which, if the result isn't json, won't give you much to go on.

amielkedev's avatar

Thank you for the response! I'm currently at work, so not able to try that out right away, but I will see what I come up with when I dump the result without the json_decode(). I might have a moment over my lunch to give it a try.

I'm also trying to nail down what the exact issue is, whether its the communicating with the Ravelry API or an issue with cURL, I'm not too sure. I'm not very comfortable working with Guzzle, but it may be worth working with if the issue is cURL. If anything Guzzle would probably be much easier to debug.

Thanks again for the input, any and all is appreciated!

Cronix's avatar

Are you caching your config for production (always good to do) using artisan config:cache? As per the docs, you shouldn't be using env() outside of config files. You should be calling config() to get the values, not env().

It's just good practice, whether you're caching the config or not, to only use env() inside of config files. That way, if you one day choose to cache the config to get the extra speed performance, everything will just work. Once the config is cached, laravel reads from the cache file and ignores env() calls. I suspect that's what's happening here.

Just create a /config/ravelry_api.php file:

return [
    'ACCESS_KEY' =>  env('RAVELRY_API_ACCESS_KEY'),
    'PERSONAL_KEY' => env('RAVELRY_API_PERSONAL_KEY'),
    'URL' => env('RAVELRY_API_URL')
];

Then instead of:

$login = env('RAVELRY_API_ACCESS_KEY');
$password = env('RAVELRY_API_PERSONAL_KEY');
$url = 'https://api.ravelry.com/patterns/search.json?designer=Handiworks+LTD';

use

$login = config('ravelry_api.ACCESS_KEY');
$password = config('ravelry_api.PERSONAL_KEY');
$url = config('ravelry_api.URL');
// ...

I also added the URL to the config as it seems appropriate, so you'd need to add RAVELRY_API_URL to your env file...

amielkedev's avatar

Thank you for the suggestion! I'm still pretty new with Laravel, so there's a fair bit I don't know as far as best practices go, especially involving the finer aspects of the framework. I did move those items over to a new config file and once again that page works fine in a local environment, but still has issues on the production server...

I also dd'd that curl_exec($ch) on both my local and production environment. On my local environment it spits out the json file as expected, but in production all I get is false, which probably isn't good.

Cronix's avatar

For now, I'd hardcode the values instead of using env() just to make sure that isn't the issue.

Cronix's avatar

Also try running artisan config:clear on your production box after you make changes

amielkedev's avatar

Also did artisan config:clear and that didn't seem to change matters. I'm just so at a loss as to what the issue is...

amielkedev's avatar

I just checked the debug logs on the Ravelry API, and the calls from my local environment are being received, but the ones from my production box are not, so one way or another it's a problem between the connection of the two, which could be any number of things.

I event attempted a curl request using the same parameters from the terminal on the server, and that worked fine and produced json.

Please or to participate in this conversation.