php - 使用 cURL PHP 获取 Instagram 公共 JSON 提要
问题描述
我想使用 cURL 从公共可用的 Instagram 配置文件中获取 JSON 响应。我坚持响应,因为如果我尝试处理var_dump
数据,它将什么都没有,一个空白字符串。我的代码有问题吗?
$url = 'https://instagram.com/'.$instance['username'].'/?__a=1';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Accept: application/json') );
$data = curl_exec($ch);
curl_close($ch);
var_dump( $url );
该$instance
变量是使用 wordpress 小部件输入表单设置的,并且是正确的。我的问题只在于 curl 行为。
谢谢您的帮助
解决方案
该脚本需要CURLOPT_FOLLOWLOCATION
设置一个参数。
$url = 'https://instagram.com/'. $instance['username'] .'/?__a=1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json') );
$data = curl_exec($ch);
curl_close($ch);
var_dump($data); // Show the response
推荐阅读
- android - 使用 Android.mk 从 com.google.android.material 构建 AppBarLayout
- automapper - Automapper:使用参数映射配置文件内的对象
- postgresql - 如何使用 Spring SimpleJdbcCall 调用 PostgreSQL 存储过程并从返回的 refcursor 获取数据
- excel - 如何根据数据库查询结果的大小缩小或扩展表
- rest - 如何在 nuxt js 的 8080 端口上配置在 localhost 上运行的 API URL?
- javascript - 计划 JS 中 NativeScript 的健康数据插件
- java - 将 kotlin 程序编译为采用 args[0] 并在控制台中打印的 java jar 后,如何在 bash 中转义 * 乘法符号?
- java - 使用 java 以 Rest Assured 方式发送 API 标头
- javascript - puppeteer browser.new_page 不是函数
- installation - 安装/使用博览会的问题