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?
7
  • 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
1

It seem I am very late for this question, but I post my experience to here anyway, and hope it can help anyone with similar experience, since it is so hard to google an answer for this kind of rare situation.

I have a very similar problem. I tested thru Browser and AJAX, they are OK, but I got connection timed out when I send the request with PHP cURL.

But my situation is a bit different. I have 2 servers, server A is the host of the API, and server B is the requesting server.

Server B is my production server that, people have been using it without any problem, until one day, I got connection timed out error from Server A.

Then, I found out that, Server A firewall blacklisted Server B, because Server B tried to perform SQL injection. (That is why browser and AJAX didn't blocked by Server A, because you are sending the request from your PC, not thru the server) So, I solved my problem by whitelisting Server B in Server A.

But the story is not end yet. The reason why server A blacklist server B was because one of my user enter a big chunk of paragraph via the API that contain the word "selected" (it is selected, not select alone) and "from", for some reason the server A marked it as SQL injection. It sound weird, but I guess the server's ModSecurity is too sensitive.

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);
4
  • 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.