Home >Backend Development >PHP Tutorial >PHP_CURL_post_get等实例

PHP_CURL_post_get等实例

WBOY
WBOYOriginal
2016-06-23 13:41:39833browse

CURL-POST

 

$url='http://localhost/Test/Curl/post.php';
//$data='name=jim&age=15';
$data=array('name'=>'jim','age'=>15);
$status=curlPost($url,$data);
echo $status;

/**
 * curl执行post发送数据
 * @param string $url 配置值
 * @param string|array $data 默认值
 * @return string
 */
function curlPost($url,$data){
    if(empty($url) || empty($data))return false;
    if(is_array($data))$data=http_build_query($data);

    $re=curl_init();//实例化cURL
    curl_setopt($re, CURLOPT_HEADER, 0);//0关闭打印相应头,直接打印需为1,
    curl_setopt($re, CURLOPT_RETURNTRANSFER, 1);//0获取后直接打印出来
    curl_setopt($re, CURLOPT_URL, $url);//初始化路径
    curl_setopt($re, CURLOPT_POST, 1);//启用时会发送一个常规的POST请求,
    curl_setopt($re, CURLOPT_POSTFIELDS, $data);//使用HTTP协议中的"POST"操作来发送的数据
    $result=curl_exec($re);//执行一个cURL会话,返回响应结果
    curl_close($re);//关闭cURL会话
    return $result;
}
?>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn