2017-01-30 42 views
1

我試圖使用laravel-wp-apia blog獲取帖子。當我使用Postmanhttp://idareyou.ee/blog//wp-json/wp/v2/posts時,我得到一個200 OK HTTP responsePostman顯示JSON結果。在瀏覽器中Laravel Wordpress JSON REST API給出奇怪的捲曲錯誤

以下LaravelBlogControllergetPosts()方法打印此Curl錯誤:

{"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 ); 
    } 
} 

別處在我的應用我成功地提取使用以下從Instagram的API的一些照片。我是否需要在我的BlogController中使用類似的「fetchData」功能?

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 

有人可以給我任何提示,我在做什麼錯?

回答

1

我會檢查此服務的配置文件 - 我的猜測是您需要爲您的呼叫設置端點(博客域)。所以一旦你運行了php artisan vendor:publish你應該在app/config下有一個特定的配置文件 - 查看是否有需要更改的設置。

希望這會有所幫助!

+0

謝謝,我認爲你是對的。我將終端設置爲''endpoint'=>'http://idareyou.ee/blog//wp-json /',現在我得到一個'404錯誤''{「error」:{「message」: 「客戶端錯誤:404」,「代碼」:404},「結果」:[],「total」:0,「pages」:0}' –

+0

Right - 你是否也調整過API調用?我懷疑你現在只是傳入你的相對路徑:'$ posts = WpApi :: posts('wp/v2/posts');'(另外,爲了安全起見,我會刪除端點中的雙斜線, 「wp-json」:「http://idareyou.ee/blog/wp-json/」) – MacPrawn

+0

謝謝。我現在有端點作爲''端點'=>'http://idareyou.ee/blog/wp-json /','和你建議的調用'$ posts = WpApi :: posts('wp/v2/';'但是我仍然得到'{「error」:{「message」:「客戶端錯誤:404」,「code」:404},「results」:[],「total」:0, :0}' –