0

I'm using a WordPress REST API to get contents of a page. The URL I'm using is:

https://sitename.org/wp-json/wp/v2/pages/4322

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:

  1. Why does it work via a browser then?
  2. Do I have to use some kind of authentication while calling an API via cURL?
  • it can be a server problem. try to execute echo file_get_contents("https://sitename.org"); – mmm Dec 4 '17 at 9:12
  • try removing the timeout – inarilo Dec 4 '17 at 9:16
  • 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. – asprin Dec 4 '17 at 9:17
  • 1
    @MarkKaplun So if the problem is somewhere else, I'm to be blamed for that? Not the correct way to use a "downvote" I would say. – asprin Sep 15 '18 at 16:10
  • 1
    Are you kidding me? "Does not fit the site?" The problem is about WP on a site dedicated for WP. Do I go and post it in Hardware? Admit you made a mistake and move on. – asprin Sep 15 '18 at 16:28
0

You can use the following code and should work, if not we should see the server configuration to see why. Also, we can check the curl_error for hints.

$curl = curl_init();
$page_id = 4322;
$wp_api_url = "https://sitename.org/wp-json/wp/v2/pages/".$page_id;     
curl_setopt_array($curl, array(
  CURLOPT_URL => $wp_api_url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
  • Same result. curl_erro gives the same message couldn't connect to host – asprin Dec 4 '17 at 9:33
  • Verify that the URL is correct. Try this $ch = curl_init("https://demo.wp-api.org/wp-json/wp/v2/pages/1"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $data = curl_exec($ch); print_r($data); to see if you can see the return. Otherwise I can speculate maybe some firewall proxy blocking you . – Drupalizeme Dec 4 '17 at 9:38
  • That was a good idea. But still no luck. Same error. – asprin Dec 4 '17 at 9:41
  • If you are not under proxy, or some misconfigure php/server , I see no other solution than you contact your hosting provider. – Drupalizeme Dec 4 '17 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.