使用php从instagram读取JSON _a=1

使用php从instagram读取JSON _a=1,php,json,instagram,Php,Json,Instagram,原创 我想首先强调的是,这是我在PHP中的第一个脚本,所以很多东西都可以改进,但现在我只需要它来工作! 我用php创建此脚本是为了从位于{{username}/?的公共instagram json文件中获取公共配置文件信息 在本地尝试,一切正常,但将其托管在网站文件“获取内容($url)”上不起作用(第29行),我尝试使用CURL读取该文件,但无论如何都不起作用,它无法正确读取json文件,试图对他读取的内容进行回显,instagram徽标显示在网站屏幕上。 我怎样才能解决它 更新 我刚刚注意到

原创

我想首先强调的是,这是我在PHP中的第一个脚本,所以很多东西都可以改进,但现在我只需要它来工作! 我用php创建此脚本是为了从位于{{username}/?的公共instagram json文件中获取公共配置文件信息 在本地尝试,一切正常,但将其托管在网站文件“获取内容($url)”上不起作用(第29行),我尝试使用CURL读取该文件,但无论如何都不起作用,它无法正确读取json文件,试图对他读取的内容进行回显,instagram徽标显示在网站屏幕上。 我怎样才能解决它

更新

我刚刚注意到,如果我尝试将任何配置文件www.instagram.com/USERNAME的链接的file\u get\u contents()设置为,它会给出完全相同的结果,可能是尝试读取www.instagram.com/USERNAME/?\u a=1 instagram通知并将我重定向到配置文件页面

我已经对通过文件\u get\u内容接收的数据尝试了htmlentities()。。。塔坦。。事实上,脚本读取的是一个奇怪的html页面,在我提供的地址中找不到它

<?php

$commentiPost;
$likePost;
$postData;
$image;
$urlprofilo;
$followers;
$username;
$follow;
$like;
$commenti;

function getMediaByUsername($count) {
global $image;
global $commentiPost;
global $likePost;
global $urlprofilo;
global $followers;
global $username;
global $follow;
global $postData;
global $like;
global $commenti;
$uname      = htmlspecialchars($_GET["name"]);
$username   = strtolower(str_replace(' ','_',$uname));
$url        = "https://www.instagram.com/".$username."/?__a=1";

$userinfo   = file_get_contents($url);
$userdata   = json_decode($userinfo,true);
$user       = $userdata['graphql']['user'];
$iteration_url = $url;



if(!empty($user)){

    $followers  = $user['edge_followed_by']['count'];
    $follow     = $user['edge_follow']['count'];
    $fullname   = $user['full_name'];
    $username   = $user['username'];
    $profilepic = $user['profile_pic_url'];
$profilepic = (explode("/",$profilepic));
$urlprofilo = "https://scontent-frt3-1.cdninstagram.com/v/t51.2885-19/s150x150/$profilepic[6]";


    $limit      = $count;
    $tryNext    = true;
    $found      = 0;


    while ($tryNext) {
        $tryNext = false;

        $remote = file_get_contents( $iteration_url );

        $response = $remote;

        if ($response === false) {
            return false;
        }
        $data = json_decode($response, true);

        if ( $data === null) {
            return false;
        }
        $media = $data['graphql']['user']['edge_owner_to_timeline_media'];

        foreach ( $media['edges'] as $index => $node ) {
            if ( $found + $index < $limit ) {
                if (isset($node['node']['is_video']) && $node['node']['is_video'] == true) {
                    $type = 'video';
                } else {
                    $type = 'image';
                }
                    $like = $like + $node['node']['edge_liked_by']['count'];
        $commenti = $commenti + $node['node']['edge_media_to_comment']['count'];
                    $image[] = array( "<a href=".$node['node']['display_url'].">
                                    <img src=".$node['node']['display_url']." alt="." />
                                    <h3>Like: </strong>".$node['node']['edge_liked_by']['count']."</strong>    Commenti: <strong>".$node['node']['edge_media_to_comment']['count']."</strong></h3>
                                </a>");
                    $postData[] = array(" '".gmdate("d-m-Y",$node['node']['taken_at_timestamp'])."',");
                  $likePost[] = array(" ".$node['node']['edge_liked_by']['count'].",");
                $commentiPost[] = array(" ".$node['node']['edge_media_to_comment']['count'].",");

            }
        }

        $found += count($media['edges']);


        if ( $media['page_info']['has_next_page'] && $found < $limit ) {
            $iteration_url = $url . '&max_id=' . $media['page_info']['end_cursor'];
            $tryNext = true;
        }
    }






} else{




}

}
getMediaByUsername( 12);

if(isset($image))
{
   $postTot = count($image);
}
else {
    $postTot = 0;
}
if($postTot > 0 and $followers > 0){
$ER = round(((($like + $commenti)/$postTot)/$followers)*100, 1);
}
else {
    $ER = 0;
}




?>

我相信这就是SSL证书的问题。当您将函数修改为:

function url_get_contents ( $url ) {
    if ( ! function_exists( 'curl_init' ) ){
        die( 'The cURL library is not installed.' );
    }

    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    // curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
    // curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec( $ch );

    if(curl_errno( $ch )) {
        die ('Curl error: ' . curl_error($ch));
    }

    curl_close( $ch );
    return $output;
}
您可能会看到这样的结果:
Curl错误:SSL证书问题:无法获取本地颁发者证书

将该证书添加到您的系统中,或使用选项取消注释行:
CURLOPT\u SSL\u VERIFYHOST
CURLOPT\u SSL\u VERIFYHOST

如果您说该证书读取不正确。您是否从url获得任何响应/数据?你得到了什么样的回应?那卷曲请求在哪里@bestprogrammerintheworld我所知道的查看是否得到任何响应的唯一方法是在页面中心回显$userinfo变量(第29行)。instagram图标显示,网页标题更改为“instagram login”@VirCom我尝试了“function url\u get\u contents($url){如果(!function\u存在('curl\u init')){die('cURL library'not installed.');}$ch=cURL_init();cURL_setopt($ch,CURLOPT_URL,$URL);cURL_setopt($ch,CURLOPT_RETURNTRANSFER,true);$output=cURL_exec($ch);cURL_close($ch);return$output;}`而不是file_get_contents($URL)嗯……我将代码粘贴到在线编辑器中并在线共享:并且可以工作并返回预期的JSON数据……这怎么可能呢?我创建了一个文件,在其中复制了您发送的代码!还有一个文件,我在其中创建了url文件获取内容(),这些是与cURL输出的链接,我没有得到任何输出提示,这怎么可能?我认为这是cURL扩展问题。您在php错误日志文件或系统日志文件中看到任何信息吗?基于cURL的函数返回任何值或停止执行其中一行中的脚本并死亡?