<?php
<?php
function request_post($url = '', $param = '') {
if (emptyempty($url) || emptyempty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$curl = curl_init();//初始化curl
curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($curl);//运行curl
curl_close($curl);
return $data;
}
POST调用方法:
以上实践成功且来自:http://ai.baidu.com/docs#/Auth/top
curl_post:
if($mtype == 1){
$url_get_str = "msgid=".$p->lastInsertId()."&uid=".$toid."&touid=".$fromid."&type=".$mtype."&connection=0&content=".$msg;
}
if($mtype == 2){
$url_get_str = "msgid=".$p->lastInsertId()."&uid=".$fromid."&touid=".$toid."&type=".$mtype."&connection=0&content=".$msg;
}
//file_put_contents("/tmp/debug.txt",$url_get_str);
//exit;
$url=$curl_url_info["save_msg"];
if(curl_post($url,$msg)=== false)
{
file_put_contents($curl_url_info["error_log_file"],$url,FILE_APPEND);
//记日志
}
$url_get_str = "msgid=".$p->lastInsertId()."&uid=".$toid."&touid=".$fromid."&type=".$mtype."&connection=0&content=".$msg;
}
if($mtype == 2){
$url_get_str = "msgid=".$p->lastInsertId()."&uid=".$fromid."&touid=".$toid."&type=".$mtype."&connection=0&content=".$msg;
}
//file_put_contents("/tmp/debug.txt",$url_get_str);
//exit;
$url=$curl_url_info["save_msg"];
if(curl_post($url,$msg)=== false)
{
file_put_contents($curl_url_info["error_log_file"],$url,FILE_APPEND);
//记日志
}
GET调用方法:
curl():
if($type==2)
{
$url=$curl_url_info["delete_msg"]."&id=".$msgid."&type=".$mtype."&uid=".$uid;
if(curl($url)=== false)
{
file_put_contents($curl_url_info["error_log_file"],$url,FILE_APPEND);
//记日志
}
}
再来一个:
关于curl的调用,可参照如下我写的方法:
function curl_request($url,&$re,$timeout=10) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$re = curl_exec($ch);
curl_close($ch);
if($re) {
return true;
} else {
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$re = curl_exec($ch);
curl_close($ch);
if($re) {
return true;
} else {
return false;
}
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1558/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2018-7-16 21:43
评论列表