【问题标题】:Laravel Wordpress JSON REST API gives strange Curl errorLaravel Wordpress JSON REST API 给出奇怪的 Curl 错误
【发布时间】:2017-06-16 03:53:03
【问题描述】:

我正在尝试使用laravel-wp-api 来获取来自a blog 的帖子。当我将Postmanhttp://idareyou.ee/blog//wp-json/wp/v2/posts 一起使用时,我得到200 OK HTTP responsePostman 显示JSON 结果。

下面的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

谁能给我一些关于我做错了什么的提示?

【问题讨论】:

    标签: php json wordpress laravel curl


    【解决方案1】:

    我会检查这个服务的配置文件——我猜你需要为你的调用设置端点(博客域)。所以一旦你运行php artisan vendor:publish,你应该在 app/config 下有一个特定的配置文件——看看那里是否有你需要更改的设置。

    希望这会有所帮助!

    【讨论】:

    • 谢谢,我认为你是对的。我将端点设置为'endpoint' =&gt; ' http://idareyou.ee/blog//wp-json/',,现在我得到一个404 error{"error":{"message":"Client error: 404","code":404},"results":[],"total":0,"pages":0}
    • 对 - 不过,您是否也调整了 API 调用?我怀疑你现在只是传入你的相对路径:$posts = WpApi::posts('wp/v2/posts');(另外,为了安全起见,我会删除你的端点中的双斜杠,在 "wp-json": "idareyou.ee/blog/wp-json/") 之前
    • 谢谢。我现在有'endpoint' =&gt; 'http://idareyou.ee/blog/wp-json/', 的端点和你建议的电话$posts = WpApi::posts('wp/v2/posts'); 但我仍然得到{"error":{"message":"Client error: 404","code":404},"results":[],"total":0,"pages":0}
    • 对不起,不熟悉 laravel-wp-api 服务,所以和你一起发现......我认为你甚至不需要那里的 url。所以试试这个:'endpoint' =&gt; 'http://idareyou.ee/blog/wp-json/wp/v2/',,然后是$posts = WpApi::posts();—— ApApi::posts 调用的唯一参数是页码。 (如获取第 2 页,$posts = WpApi::posts(2);
    • 太棒了!你是一个救生员。这样可行。干杯
    猜你喜欢
    • 2017-07-19
    • 1970-01-01
    • 2017-11-11
    • 2015-06-01
    • 2012-05-21
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多