Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Why my Restful API call not working in localhost? [SOLVED]


Go to End


7 Posts   1682 Views

Avatar
VPull

Community Member, 58 Posts

6 June 2017 at 5:29pm

Edited: 06/06/2017 5:30pm

Hi guys,

When I call my API URL in localhost it gives 500 error and same code working on the live server.
even I tried to call URL using ajax and it works fine on localhost.

my SilverStripe code

$service = RestfulService::create('URL');
        $response = $service->request('');
        
        echo $response->getStatusCode();

and ajax code

$(document).ready(function(){
		
                        var url = 'https://abc.com/HotelDetails?Id=45';

                        jQuery.ajax({
                          url: url,
                          method: 'GET'
                        }).then(function(data) {
                          console.log(data);
                        });


                });

what I found in Chrome developer tools

Sorry, I am not able to give API URL

Avatar
martimiz

Forum Moderator, 1391 Posts

22 June 2017 at 1:25am

Could be all kinds of things, and a 500 error isn't very specific...

Have you tried putting your site in dev mode? That might give you the actual errormessages (https://docs.silverstripe.org/en/3/developer_guides/debugging/environment_types/#environment-types)

Or else look into the PHP error files on your local machine

Could be you don't have PHP curl installed...

Could be $response->getStatusCode() fails because $response is empty.... All kinds of things...

Avatar
VPull

Community Member, 58 Posts

22 June 2017 at 5:54pm

Edited: 22/06/2017 5:56pm

Thanks martimiz for your reply

I already setup site in dev mode. I also checked error log but there is nothing related to this and I have php curl installed on my machine
https://postimg.org/image/rrbd6h8pr/

Avatar
martimiz

Forum Moderator, 1391 Posts

22 June 2017 at 10:53pm

Some basic tips that may help:

- Compare the PHP versions on your production and your local server - that may give a clue

- Maybe setting up logging in SilverStripe will help uncover the actual error message: https://docs.silverstripe.org/en/3.3/developer_guides/debugging/error_handling/#filesystem-logs

- check your php error settings, maybe you restricted the display of error messages..?

- sometimes the screen will give you a 500 error, but tools like Firebug (net) will let you read the server answer that quite often has the actual error messages (I use that a lot)

- very basic, remove pieces of code until the error message disappears, so you see what brings the error - it may even be something else in your code... Try var_dump() for the result values.

Avatar
VPull

Community Member, 58 Posts

27 June 2017 at 10:30pm

Hi martimiz, I followed your suggestion and even updated my local php version same as hosting server.
Still no success. The only thing I get after var_dump request variable.

object(RestfulService_Response)#82 (6) { ["simpleXML":protected]=> NULL ["cachedResponse":protected]=> bool(false) ["statusCode":protected]=> int(500) ["statusDescription":protected]=> string(21) "Internal Server Error" ["headers":protected]=> array(0) { } ["body":protected]=> bool(false) } 

Avatar
VPull

Community Member, 58 Posts

3 July 2017 at 8:45pm

Edited: 03/07/2017 8:50pm

In my case it is regarding certificate issue, so I tried to request api using plain php and curl.

$ch = curl_init();

curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_SSL_VERIFYPEER => true,
    CURLOPT_CERTINFO => true,
    CURLOPT_URL => 'https://your-api-url.com/api/',
    
));

if(!curl_exec($ch)){
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}else{
  $resp = curl_exec($ch);
  var_dump($resp);
}

curl_close($ch);

and it gives error

SSL certificate problem: unable to get local issuer certificate

So I follow the steps from here

Avatar
nutanbharti

Community Member, 1 Post

6 July 2017 at 10:47pm

Thank All of YOu