为什么 php "curl" 函数在第二次调用时不能正常工作?
Why is does php "curl" function not work correctly when called a second time?
我是第一次使用 php Curl
这是我的代码:
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://huger.blog.ir/rss/');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$full = curl_exec($ch);
curl_close($ch);
$full = (string)$full;
$l = strpos($full,'</link>');
$start = strpos($full,'<link>',$l);
$end = strpos($full,'</link>',$l+2);
global $link;
$link = substr($full, $start , $end-$start);
$v = curl_init();
curl_setopt($v,CURLOPT_URL,$link);
curl_setopt($v,CURLOPT_RETURNTRANSFER,true);
$page = curl_exec($v);
curl_close($v);
echo $page;
$ch
curl 完成了它的工作并且 $link
正确完成了。
现在我认为我的第二个 curl ($v
) 不起作用。
有人可以帮忙吗?
在这种情况下,如果我能让一个元素工作,我会将该工作元素包装在 function
或 class
中以便按原样重用。看看这是否是您要找的:
function cURL($url = false,$settings = false)
{
$string = (!empty($settings['toString']));
$json = (!empty($settings['toJson']));
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://huger.blog.ir/rss/');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);
if($string)
$response = (string) $response;
elseif($json)
$response = json_decode($response,true);
curl_close($ch);
return $response;
}
function toLink($cURL = false)
{
$l = strpos($cURL,'</link>');
$start = strpos($cURL,'<link>',$l);
$end = strpos($cURL,'</link>',$l+2);
$link = substr($cURL, $start , $end-$start);
// As noted by @Hari Swaminathan, you have a <link> at the front,
// so strip_tags will remove that
return strip_tags($link);
}
$link = cURL('http://huger.blog.ir/rss/',array("toString"=>true));
echo cURL(toLink($link));
试试这个代码
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://huger.blog.ir/rss/');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$full = curl_exec($ch);
curl_close($ch);
$full = (string)$full;
$l = strpos($full,'</link>');
$start = strpos($full,'<link>',$l);
$end = strpos($full,'</link>',$l+2);
global $link;
$link = substr($full, $start , $end-$start);
var_dump($link);
$v = curl_init();
curl_setopt($v,CURLOPT_URL,$link);
curl_setopt($v,CURLOPT_RETURNTRANSFER,true);
$page = curl_exec($v);
echo curl_error($v);
curl_close($v);
var_dump($page);
你可以看到你的url前有一个标签
string '<link>http://huger.blog.ir/1394/08/24/start' (length=43)
这是导致问题的原因
我是第一次使用 php Curl 这是我的代码:
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://huger.blog.ir/rss/');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$full = curl_exec($ch);
curl_close($ch);
$full = (string)$full;
$l = strpos($full,'</link>');
$start = strpos($full,'<link>',$l);
$end = strpos($full,'</link>',$l+2);
global $link;
$link = substr($full, $start , $end-$start);
$v = curl_init();
curl_setopt($v,CURLOPT_URL,$link);
curl_setopt($v,CURLOPT_RETURNTRANSFER,true);
$page = curl_exec($v);
curl_close($v);
echo $page;
$ch
curl 完成了它的工作并且 $link
正确完成了。
现在我认为我的第二个 curl ($v
) 不起作用。
有人可以帮忙吗?
在这种情况下,如果我能让一个元素工作,我会将该工作元素包装在 function
或 class
中以便按原样重用。看看这是否是您要找的:
function cURL($url = false,$settings = false)
{
$string = (!empty($settings['toString']));
$json = (!empty($settings['toJson']));
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://huger.blog.ir/rss/');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);
if($string)
$response = (string) $response;
elseif($json)
$response = json_decode($response,true);
curl_close($ch);
return $response;
}
function toLink($cURL = false)
{
$l = strpos($cURL,'</link>');
$start = strpos($cURL,'<link>',$l);
$end = strpos($cURL,'</link>',$l+2);
$link = substr($cURL, $start , $end-$start);
// As noted by @Hari Swaminathan, you have a <link> at the front,
// so strip_tags will remove that
return strip_tags($link);
}
$link = cURL('http://huger.blog.ir/rss/',array("toString"=>true));
echo cURL(toLink($link));
试试这个代码
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://huger.blog.ir/rss/');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$full = curl_exec($ch);
curl_close($ch);
$full = (string)$full;
$l = strpos($full,'</link>');
$start = strpos($full,'<link>',$l);
$end = strpos($full,'</link>',$l+2);
global $link;
$link = substr($full, $start , $end-$start);
var_dump($link);
$v = curl_init();
curl_setopt($v,CURLOPT_URL,$link);
curl_setopt($v,CURLOPT_RETURNTRANSFER,true);
$page = curl_exec($v);
echo curl_error($v);
curl_close($v);
var_dump($page);
你可以看到你的url前有一个标签
string '<link>http://huger.blog.ir/1394/08/24/start' (length=43)
这是导致问题的原因