Hi all.
I have a working CURL request that calls an API for us and this works great. I have since been trying to convert it into ExternalResourceClient but am having problems with the main error being:
Request failed: file get contents([IPADDRESS]): Failed to open stream: Network is unreachable
I've checked with my server provider and they have made sure that allow_url_fopen is configured correctly and they say it is. Can anyone see any reason why I'd be getting the error?
Original Code is a standard CURL call with these options:
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
New Code:
use Sugarcrm\Sugarcrm\Security\HttpClient\ExternalResourceClient; use Sugarcrm\Sugarcrm\Security\HttpClient\RequestException; // Define the endpoint and authorization token $url = "https://api.erpdev.accessacloud.com/financials-graphql"; $authToken = ""; // Define the GraphQL query $query = '{"query":"query{debtors{lists{customers{items{name}}}}}"}' // Create the ExternalResourceClient instance $client = new ExternalResourceClient(); try { echo "Attempting to send request..." . "<br>" . PHP_EOL; // Send the request $response = $client->post($url, $query, [ 'Content-Type' => '', 'X-api-token' => '', 'Ocp-Apim-Subscription-Key' => '', 'Authorization' => 'Bearer ' . $authToken, ]); echo "Request sent successfully." . "<br>" . PHP_EOL; } catch (RequestException $e) { echo "Request failed: " . $e->getMessage() . "<br>" . PHP_EOL; throw new \SugarApiExceptionError($e->getMessage()); } $parsed = !empty($response) ? json_decode($response->getBody()->getContents(), true) : null; // Echo the raw response body and parsed response for debugging if (!empty($response)) { echo "Raw Response: " . $response->getBody()->getContents() . "<br>" . PHP_EOL; } else { echo "No response received." . "<br>" . PHP_EOL; } if ($parsed) { echo "Parsed Response: " . print_r($parsed, true) . "<br>" . PHP_EOL; } else { echo "Failed to parse response." . "<br>" . PHP_EOL; }
Any ideas? Our server provider said the following:
The certificate is invalid, you can't get a certificate for an IP address. If you're using the DNS name (which I think is ) you'll get a synthesized IPv6 address to allow you to connect (There's no direct IPv4 in our platform). Can you check with Sugar that they'll connect to the IPv6 address in preference to the IPv4 address that's returned on the DNS lookup
Thanks!