使用PHP的curl扩展实现跨域post请求,以及file_get_contents()百度短网址例子
2014-04-15 16:58
1106 查看
<?php $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,"http://dwz.cn/create.php"); curl_setopt($ch,CURLOPT_POST,true); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); $data=array('url'=>'http://www.cnblogs.com/zuoxiaobing/'); curl_setopt($ch,CURLOPT_POSTFIELDS,$data); $strRes=curl_exec($ch); curl_close($ch); $arrResponse=json_decode($strRes,true); if($arrResponse['status']==0) { /**错误处理*/ echo iconv('UTF-8','GBK',$arrResponse['err_msg'])."\n"; } /** tinyurl */ echo$arrResponse['tinyurl']."\n"; ?>
2.使用file_get_contents();
<?php $data=array( 'url'=>'http://www.cnblogs.com/zuoxiaobing/' ); $data = http_build_query($data); $opts = array('http' => array( 'method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded\r\n" ."Content-Length: " . strlen($data) . "\r\n", // "Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n", 'content' => $data, 'timeout' => 60 ) ); $context = stream_context_create($opts); $url = 'http://dwz.cn/create.php'; $result = file_get_contents($url, false, $context); echo $result;
相关文章推荐
- PHP发送POST请求的三种方式 分别使用curl file_get_content fsocket 来实现post提交数据
- PHP中使用CURL实现GET和POST请求
- PHP中使用cURL实现Get和Post请求
- PHP中使用CURL实现GET和POST请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用CURL实现GET和POST请求(详细)
- PHP中使用cURL实现Get和Post请求的方法
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用file_get_contents post数据代码例子
- php 模拟浏览器 发送 post 和 get 请求(兼容curl + file_get_contents)
- cURL和file_get_contents实现模拟post请求
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用CURL实现GET和POST请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用CURL实现GET和POST请求
- 在PHP中使用CURL实现GET和POST请求的方法
- PHP中使用cURL实现Get和Post请求的方法