我需要使用PHP(file_get_contents
)调用第三方API ,对json响应(json_decode
)进行解码,然后将结果插入网页中。
我想验证所有这些步骤都不会允许受损的API服务器在我的服务器上执行任意代码。
对于此问题,可以返回恶意HTML / JS是可以的-我的问题严格是关于在服务器端执行任意PHP代码或系统命令。
谢谢你。
编辑:这是一个代码示例。
<?php
$API_URL = 'https://HARDCODED.URL/SOMETHING';
$response = file_get_contents($API_URL);
$content = json_decode($response);
$server_address = $content->{'server_address'};
echo $server_address;
?>
根据OP的要求:
如何转换file_get_contents()
为curl请求:
<?php
// init the CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // return response as a string
curl_setopt($ch, CURLOPT_URL, 'https://HARDCODED.URL/SOMETHING'); // the URL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); // verify SSL info
// You MIGHT need this step or else the CURLOPT_SSL_VERIFYPEER will cause issues
//
// Download https://curl.haxx.se/ca/cacert.pem and save as cacert.pem
//
// curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cacert.pem');
//
// I say MIGHT because your webhost might have already set CURLOPT_CAINFO in the php.ini
// Get JSON
$result = curl_exec($ch);
// Basic error handling
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200)
{
$content = json_decode($result, TRUE);
}
else
{
echo 'Something went wrong :( please try again later.';
}
// Close connection
curl_close($ch);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句