如何使用curl多次包含一个PHP页面(How to include a PHP page more than once one with a curl)

编程入门 行业动态 更新时间:2024-10-20 16:27:11
如何使用curl多次包含一个PHP页面(How to include a PHP page more than once one with a curl)

如果我PHP不止一次包含此页面。

$json_file = @curl("http://api.justin.tv/api/stream/list.json?channel={$stream}"); $json_array = json_decode($json_file, true); if (strtolower($json_array[0]['name']) == strtolower("live_user_{$stream}")) { echo '<img src="http://www.pvpallday.com/streams/' . $stream . '_on.gif" />'; } else { echo 'off'; } function curl($url, $post = null, $retries = 3) { $curl = curl_init($url); if (is_resource($curl) === true) { curl_setopt($curl, CURLOPT_FAILONERROR, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); if (isset($post) === true) { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, (is_array($post) === true) ? http_build_query($post, "", "&") : $post); } $result = false; while (($result === false) && ( --$retries > 0)) { $result = curl_exec($curl); } curl_close($curl); } return $result; }

关于在页面上多次使用卷曲我收到错误。 有没有让它发生在一个页面上仍然有多个包括它?

If I PHP include this page more than once.

$json_file = @curl("http://api.justin.tv/api/stream/list.json?channel={$stream}"); $json_array = json_decode($json_file, true); if (strtolower($json_array[0]['name']) == strtolower("live_user_{$stream}")) { echo '<img src="http://www.pvpallday.com/streams/' . $stream . '_on.gif" />'; } else { echo 'off'; } function curl($url, $post = null, $retries = 3) { $curl = curl_init($url); if (is_resource($curl) === true) { curl_setopt($curl, CURLOPT_FAILONERROR, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); if (isset($post) === true) { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, (is_array($post) === true) ? http_build_query($post, "", "&") : $post); } $result = false; while (($result === false) && ( --$retries > 0)) { $result = curl_exec($curl); } curl_close($curl); } return $result; }

I get an error about having the curl on the page more than once. is there away to make that not happen and still have multiple includes of it on one page?

最满意答案

在function_exists中包装你的函数

if(!function_exists('curl')) { function curl($url, $post = null, $retries = 3) {

请注意,您获得的错误与curl无关。 它发生在你的书面函数curl在第一个include定义,当执行另一个include时,php尝试再次重新定义该函数。

Wrap your function in function_exists

if(!function_exists('curl')) { function curl($url, $post = null, $retries = 3) {

Note that the error you are getting is not related to curl. It happens as your written function curl is defined in first include and when another include is performed php tries to redefine that function again.

更多推荐

本文发布于:2023-08-06 17:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   页面   curl   PHP   include

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!