Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel Wordpress JSON REST API给出了奇怪的Curl错误_Php_Json_Wordpress_Laravel_Curl - Fatal编程技术网

Php Laravel Wordpress JSON REST API给出了奇怪的Curl错误

Php Laravel Wordpress JSON REST API给出了奇怪的Curl错误,php,json,wordpress,laravel,curl,Php,Json,Wordpress,Laravel,Curl,我正试着用它来获取来自的帖子。当我使用Postman和http://idareyou.ee/blog//wp-json/wp/v2/posts我得到一个200OK HTTP响应,Postman显示JSON结果 以下LaravelBlogControllergetPosts()方法在浏览器中打印此Curl错误: {“error”:{“message”:“cURL error 6:无法解析主机'\u003Cwp\u location\u003E'(请参阅http:\/\/cURL.haxx.se\/

我正试着用它来获取来自的帖子。当我使用
Postman
http://idareyou.ee/blog//wp-json/wp/v2/posts
我得到一个
200OK HTTP响应
Postman
显示
JSON
结果

以下
Laravel
BlogController
getPosts()
方法在浏览器中打印此
Curl
错误:

{“error”:{“message”:“cURL error 6:无法解析主机'\u003Cwp\u location\u003E'(请参阅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  );
  }
}

有人能告诉我我做错了什么吗?

我会检查此服务的配置文件-我猜您需要为您的呼叫设置端点(博客域)。因此,一旦您运行了
php artisan vendor:publish
,您应该在app/config下有一个特定的配置文件-查看是否有需要更改的设置


希望这有帮助

谢谢,我想你是对的。我将端点设置为
'endpoint'=>'http://idareyou.ee/blog//wp-json/“,
现在我得到了一个
404错误
{“错误”:{“消息”:“客户端错误:404”,“代码:404}”,“结果”:[],“总计”:0,“页面”:0}
对-您是否也调整了API调用?我怀疑您现在只是传入了相对路径:
$posts=WpApi::posts('wp/v2/posts')(另外,为了安全起见,我会删除端点中“wp json”之前的双斜杠):)谢谢。我现在将端点设置为
'endpoint'=>http://idareyou.ee/blog/wp-json/“,
和您建议的调用
$posts=WpApi::posts('wp/v2/posts');
但我仍然得到
{“error”:{“message”:“Client error:404”,“code”“:404},“结果”:[],“总计”:0,“页面”:0}
抱歉,不熟悉laravel wp api服务,因此请与您一起探索。。。我想你甚至不需要一个网址。所以试试这个:
'endpoint'=>'http://idareyou.ee/blog/wp-json/wp/v2/“,
然后
$posts=WpApi::posts()--ApApi::posts调用的唯一参数是页码。(与第2页一样,
$posts=WpApi::posts(2);
)太棒了!你是个救生员。这很有效。干杯
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