Solved

Problem in calling API from PHP

  • 6 July 2021
  • 4 replies
  • 405 views

Userlevel 3
Badge +8

Hello Everybody,

We are running Apps10 in the cloud. With POSTMAN I can access the api that gives me back the part catalog fields. I have copied the PHP code from Postman but it doesn’t work. It gives me back this screen:

I have tried also the Javascript code and it doesn’t work either. But if I start chrome with --disable-web-security, it works.

Now the question is: what do I need to let the PHP code work succesfully ?

Thanks to anyone that can help.

icon

Best answer by luca.puccini 12 July 2021, 13:55

View original

This topic has been closed for comments

4 replies

Badge

Hi,

Can you share some more details on this with code you use to Access the service (including Authentication part)?

/Madusanka

Userlevel 3
Badge +8

Hello, here is the code. I hid the url and the authentication.

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL            => "https://xxxxx.ifs.cloud:48080/int/ifsapplications/projection/v1/PartHandling.svc/PartCatalogSet(PartNo='LUCA')",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING       => '',
  CURLOPT_MAXREDIRS      => 10,
  CURLOPT_TIMEOUT        => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST  => 'GET',
  CURLOPT_HTTPHEADER     => array(
      'Content-Type:application/json',
      'Authorization: Basic xxxxx==')
));

$response = curl_exec($curl);
if($response === false){
    echo 'Curl error: ' . curl_error($curl);
} else {
    print_r($response);
}
curl_close($curl);


?>

Thanks.

Userlevel 7
Badge +20

If it works in Postman and not in PHP, it could be due to CORS (Cross-Origin Resource Sharing) https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS where browser rejects accessing resources in a different domain.

If your php application is in a different domain, try bringing into the same domain as IFS to see if solves the problem.

 

Cheers!

Damith

Userlevel 3
Badge +8

To solve this issue just add a rule into the http response header.

Access-Control-Allow-Origin *
Note that, instead of the * that means everybody, you can add a list of knowed url.