栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php curl 发送get或者post请求

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php curl 发送get或者post请求

php可以扮演资料的发送者。

  1. 简单的get


$ch = curl_init();curl_setopt($ch, CURLOPT_URL, "http://SomeDomain/SamplePath?SomeVar=test");curl_exec($ch);curl_close($ch);


2.  简单的post


$ch = curl_init();curl_setopt($ch, CURLOPT_URL, "http://SomeDomain/SamplePath");curl_setopt($ch, CURLOPT_POST, true); // 啟用POSTcurl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( array"a"=>"123""b"=>"321") )); curl_exec($ch); curl_close($ch);


3. 高级的post,可设置多个参数


$toURL "http://SomeDomain/SamplePath?SomeVar=XX";$post array(  "a"=>"123",  "b"=>"321",);$ch = curl_init();$options array(  CURLOPT_URL=>$toURL,  CURLOPT_HEADER=>0,  CURLOPT_VERBOSE=>0,  CURLOPT_RETURNTRANSFER=>true,  CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible;)",  CURLOPT_POST=>true,  CURLOPT_POSTFIELDS=>http_build_query($post),);curl_setopt_array($ch$options);// CURLOPT_RETURNTRANSFER=true 会返回服务器响应代码,// false 时只返回成功与否;$result = curl_exec($ch); curl_close($ch);echo $result;


4. post 发送文件


发送端代码:


$target_url 'http://127.0.0.1/accept.php';        //This needs to be the full path to the file you want to send.    $file_name_with_full_path realpath('./sample.jpeg');            $post array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);             $ch = curl_init();    curl_setopt($ch, CURLOPT_URL,$target_url);    curl_setopt($ch, CURLOPT_POST,1);    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);    $result=curl_exec ($ch);    curl_close ($ch);    echo $result


接收端代码:


$uploaddir realpath('./') . '/';$uploadfile $uploaddir basename($_FILES['file_contents']['name']);echo '
';    if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {        echo "File is valid, and was successfully uploaded.n";    else {        echo "Possible file upload attack!n";    }    echo 'Here is some more debugging info:';    print_r($_FILES);    echo "nn";    print_r($_POST);print ". "e>n";


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/227597.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号