1

I'm trying to use laravel-wp-api to get the posts from a blog. When I use Postman with http://idareyou.ee/blog//wp-json/wp/v2/posts I get a 200 OK HTTP response and Postman shows the JSON result.

The following Laravel BlogController getPosts() method prints in the browser this Curl error:

{"error":{"message":"cURL error 6: Couldn't resolve host '\u003Cwp_location\u003E' (see http:\/\/curl.haxx.se\/libcurl\/c\/libcurl-errors.html)"},"results":[],"total":0,"pages":0}

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use WpApi;
class BlogController extends Controller
{
  public function getPosts()
  {

    $posts = WpApi::posts('http://idareyou.ee/blog//wp-json/wp/v2/posts');
    echo json_encode($posts,true); 

    //return view('pages.blog', ['active'=>'navBlog'])->with('posts', $posts  );
  }
}

Elsewhere in my app I am fetching successfully some pictures from Instagram API using the following. Do I need a similar 'fetchData' function in my BlogController?

function fetchData($url){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  $result = curl_exec($ch);
  curl_close($ch);
  return $result;
}

$result = fetchData("https://api.instagram.com/v1/users/.......");
$result = json_decode($result, true);
$lastFive = array_slice($result['data'], 0, 5);   // returns last 5 instagram pics

Can anybody give me any tips on what I'm doing wrong?

1 Answer 1

1

I would check the config file for this service - my guess is you need to set-up the endpoint (blog domain) for your calls. So once you run php artisan vendor:publish you should have a specific config file under app/config - see if there's a setting there you need to change.

Hope this helps!

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I think you were right. I set the endpoint to 'endpoint' => ' http://idareyou.ee/blog//wp-json/', and now I get a 404 error {"error":{"message":"Client error: 404","code":404},"results":[],"total":0,"pages":0}
Right - did you adjust your API call too, though? I suspect you now just pass in your relative path: $posts = WpApi::posts('wp/v2/posts'); (also, just to be safe, I'd remove the double slashes in your endpoint, before "wp-json": "idareyou.ee/blog/wp-json/")
Thanks. I now have the endpoint as 'endpoint' => 'http://idareyou.ee/blog/wp-json/', and the call as you suggested $posts = WpApi::posts('wp/v2/posts'); but I still get {"error":{"message":"Client error: 404","code":404},"results":[],"total":0,"pages":0}
Sorry, not familiar with the laravel-wp-api service, so discovering along with you... I think you don't even need a url there. So try this instead: 'endpoint' => 'http://idareyou.ee/blog/wp-json/wp/v2/', and then $posts = WpApi::posts(); -- the only parameter to the ApApi::posts call is a page number. (as in to get page 2, $posts = WpApi::posts(2);)
Brilliant! You're a life saver. That works. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.