Php 使用文件获取内容时出错,返回null
我制作了一个php文件,用于查找并返回Instagram帖子。我把这个php文件上传到api.mydomain.com。这是代码:Php 使用文件获取内容时出错,返回null,php,Php,我制作了一个php文件,用于查找并返回Instagram帖子。我把这个php文件上传到api.mydomain.com。这是代码: <?php $url = $_GET["url"];//Instagram Post Link $resp = file_get_contents("$url?__a=1"); $r = json_decode($resp,true); if (strpos($resp, 'edge_media_preview_like
<?php
$url = $_GET["url"];//Instagram Post Link
$resp = file_get_contents("$url?__a=1");
$r = json_decode($resp,true);
if (strpos($resp, 'edge_media_preview_like') !== false) {
$start_counter = $r['graphql']['shortcode_media']['edge_media_preview_like']['count'];
}else{
$start_counter="ERROR";
}
return $start_counter;
首先,您应该始终验证输入变量。在您的情况下,您应该验证$\u GET[“url”]
,因为任何人都可以使用您的api.mydomain.com处理不好的事情,例如DoS
下一件事是,在将url插入另一个url之前,应先对其进行转义:
$InstagramPost = "https://www.instagram.com/p/CETxYxtFq8h/";
$instaEscaped = rawurlencode($InstagramPost);
$API_LINK = "http://api.mydomain.com?url=$instaEscaped";
最后,在执行json\u解码之前,最好检查给定的响应。可能它已经为false
:
$url = $_GET["url"];
$resp = file_get_contents("$url?__a=1");
if (false === $resp) {
// do something
}
$r = json_decode($resp,true);
您的主机可能有防火墙阻止它,或者可能没有启用fopen\u url
。启用警告并查看文件\u get\u contents()
中的警告。使用
$url = $_GET["url"];
$resp = file_get_contents("$url?__a=1");
if (false === $resp) {
// do something
}
$r = json_decode($resp,true);