/*
*判断一个url是否为有效链接
*/
function isRealUrl($url){
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_NOBODY => true
);
curl_setopt_array($ch, $options);
curl_exec($ch);
if(!curl_errno($ch)){
return 200==curl_getinfo($ch,CURLINFO_HTTP_CODE)?true:false;
}
curl_close($ch);
}
$url = 'http://testpic1.tomoimg.cn/240x180/394/855/517932781/200901/12312215602409.jpg';
if(isRealUrl($url)){echo 'yes';}else{echo 'no';}
/异步请求的例子:
$userid = 517932781;
$imageid = 1520;
$albumid = 2637;
$tags = 'aa';
extract($_POST);
$url = 'http://'.$_SERVER['HTTP_HOST'].'/ajax/image.php';
$fields = array(
'userid' => $userid,
'imageid' => $imageid,
'albumid' => $albumid,
'tags' => $tags,
'optype' => 'del'
);
$ch = curl_init() ;
curl_setopt($ch, CURLOPT_URL,$url) ;
curl_setopt($ch, CURLOPT_POST,true) ;
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields) ;
$result = curl_exec($ch) ;
curl_close($ch) ;
//上传文件
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://lh.tom.com/deal/import.php');
$fields = array(
'tname' => '道德经',
'country' => 1,
'author' => '老子',
'tags' => '道德经',
'desc' => '道可道,非常道。名可名,非常名。无名天地之始。有名万物之母。故常无欲以观其妙。常有欲以观其徼。此两者同出而异名,同谓之玄。玄之又玄,众妙之门。',
'volume' => 2,
'cover' => '@'.realpath('/data/lianhuanhua/deal/1.jpg')
);
curl_setopt($ch, CURLOPT_POST, true) ;
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
$result = curl_exec($ch);
curl_close($ch);
//多文件上传
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://lh.tom.com/deal/addpic.php');
$j = 0;
$fields = array(
'vid' => 103,
'upfile['.$j++.']' => '@'.realpath('/data/lianhuanhua/deal/1.jpg'),
'upfile['.$j++.']' => '@'.realpath('/data/lianhuanhua/deal/2.jpg')
);
curl_setopt($ch, CURLOPT_POST, true) ;
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
$result = curl_exec($ch);
curl_close($ch);
当你掌握了php curl库你就能做很多你想做的事情了,呵呵,前不久玩开心网的X世界,战斗实在繁琐,我直接写了个战斗助手非常好用,本代码就不开源了 :)掌握原理一样开源实现。
网站计数器
您可能感兴趣的文章: