I'm using a WordPress REST API to get contents of a page. The URL I'm using is:
When I open that URL in browser, I'm getting back valid response.
But when I try to use that same URL via cURL, I'm getting:
couldn't connect to host
The code I'm using is this:
$page_id = 4322;
$wp_api_url = "https://sitename.org/wp-json/wp/v2/pages/".$page_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $wp_api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
if (FALSE === $response)
{
echo curl_error($ch); // <---- failing here
}
else
{
echo '<pre>';
print_r(json_decode($response, true));
echo '</pre>';
}
curl_close($ch);
Which begs the question:
- Why does it work via a browser then?
- Do I have to use some kind of authentication while calling an API via cURL?
echo file_get_contents("https://sitename.org");
file_get_contents
is banned on my server. I use OSvC (Oracle RightNow). The strange thing is making an Ajax request works too. But just not when trying to connect via server side code.