如何使用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.
更多推荐
发布评论