curl get post请求



 //多人pk赛
    public function getGroupPK($uid,$type){
        $data = array('uid'=>$uid,'type'=>$type);
        $url = STATIC_KG_URL."gamedata?".http_build_query($data);
        $ch = curl_init($url) ;
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
        $output = json_decode(curl_exec($ch),true);
        return $output;
    }


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

  1. 简单的get

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


2.  简单的post

1
2
3
4
5
6
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://SomeDomain/SamplePath");
curl_setopt($ch, CURLOPT_POST, true); // 啟用POST
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( array"a"=>"123""b"=>"321") )); 
curl_exec($ch); 
curl_close($ch);


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$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 发送文件


发送端代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$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');
        /* curl will accept an array here too.
         * Many examples I found showed a url-encoded string instead.
         * Take note that the 'key' in the array will be the key that shows up in the
         * $_FILES array of the accept script. and the at sign '@' is required before the
         * file name.
         */
    $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


接收端代码:

1
2
3
4
5
6
7
8
9
10
$uploaddir realpath('./') . '/';$uploadfile $uploaddir basename($_FILES['file_contents']['name']);echo '<pre>';
    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 "\n<hr />\n";
    print_r($_POST);print "</pr" "e>\n";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值