#post提交raw数据
 function curls($url, $data_string) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'X-AjaxPro-Method:ShowList',
        'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36'
    );
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$get_url = "http://abc.xxx.com/UC_News_ShowList,App_Web_abc.ashx";

$post_str = '{"P":5,"Sclass":"新闻","Table":"HS_N_News","Link":"News"}';

$post_datas = curls($get_url, $post_str);

echo $post_datas;

 

 /*
     * 接收json格式的数据
     * PHP默认只识别application/x-www.form-urlencoded标准的数据类型,修改头信息也没有结果…只能通过以下方式获得数据
     * */
 public function parameter()
    {
        //第一种方式
        $post = $GLOBALS['HTTP_RAW_POST_DATA'];
        p($post);
        return $post;
        //第二种方式
        #$post = file_get_contents('php://input');
    }

   

    /*
     * curl发送数据封装
     * $url: 地址
     * $data: 数组形式的post数据
     * $json: 是否以json方式提交(1: 是, 0:否)
     * */
 public function curl($url,$data= null,$json= false)
    {
        $curl = curl_init();
        //访问地址
        curl_setopt($curl,CURLOPT_URL,$url);
        //证书
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
        if(!empty($data))
        {
            //发送json格式的数据
            if($json && is_array($data))
            {
                $data = json_encode($data);
            }
        }
        //设置post传值
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        if($json)
        {
            //设置header头
            curl_setopt($curl,CURLOPT_HEADER,0);
            curl_setopt($curl,CURLOPT_HTTPHEADER,[
                'Content-Type: application/json; charset=utf-8',
                'Content-Length:' . strlen($data)
            ]);
        }
        //设为不输出
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        //执行
        $return = curl_exec($curl);
        //获取最后错误
        $error = curl_errno($curl);
        //关闭
       curl_close($curl);<BR>       if($error)<BR>       {<BR>           return['error'=>false,'errmsg'=>$error];
        }
        return $return;
    }

 /*
  * curl文件上传_关键代码_其它一样
  * */

  $dos = curl_file_create($file_name,$file['type'],$file['name']);
 curl_setopt($curl,CURLOPT_POST,1);
 @curl_setopt($curl,CURLOPT_POSTFIELDS,['media' => $dos]);

_____________________________curl提交demo_________________________________________

 

publicfunctioncurl($url,$post=''){

   /*$cart="theCityName=北京";*/
    #参数
    $ch=curl_init();
    #设置参数
    #url地址
    curl_setopt($ch,CURLOPT_URL,$url);
    #设置为不输出
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    #设置一个post提交
    curl_setopt($ch,CURLOPT_POST,1);
    #设置协议POST提交
    curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
    #是否打印header信息流
    curl_setopt($ch,CURLOPT_HEADER,0);
    #设置浏览器请求
    curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
    #页面跳转
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    #执行
    $reg=curl_exec($ch);
    #释放
    curl_close($ch);
    pr($reg);
    #把页面数据转化为对象
    /*$str=simplexml_load_string($reg);*/
}

_______________________________CURL提交demo_________________________________________

 
#需要提交的值
$psot=array();
#提交的地址
$url=".../";
#参数
$ch=curl_init();
#设置参数
#url地址
curl_setopt($ch,CURLOPT_URL,$url);
#设置为不输出
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
#设置一个post提交
curl_setopt($ch,CURLOPT_POST,1);
#设置协议POST提交
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
#是否打印header信息流
curl_setopt($ch,CURLOPT_HEADER,0);
#设置浏览器请求
curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
#页面跳转
/*
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
*/
#执行
$reg=curl_exec($ch);
#释放
curl_close($ch);
print_r($reg);
#把页面数据转化为对象
/*
$str= simplexml_load_string($reg);
*/

--------------------------------------------------------阿里云内的curl方法----------------------------------------------------------------

/*
* 提交
*
* @param url
* @param array $postFileds
* @return string $reponse
* */
private function curl($url, $postFields = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($this->readTimeout) {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
}
if ($this->connectTimeout) {
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
}
curl_setopt ( $ch, CURLOPT_USERAGENT, "dingtalk-sdk-php" );
//https 请求
if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}

if (is_array($postFields) && 0 < count($postFields))
{
$postBodyString = "";
$postMultipart = false;
foreach ($postFields as $k => $v)
{
if("@" != substr($v, 0, 1))//判断是不是文件上传
{
$postBodyString .= "$k=" . urlencode($v) . "&";
}
else//文件上传用multipart/form-data,否则用www-form-urlencoded
{
echo $k.'11111'."\n";
$postMultipart = true;
if(class_exists('\CURLFile')){
$postFields[$k] = new \CURLFile(substr($v, 1));
}
}
unset($k, $v);
}
curl_setopt($ch, CURLOPT_POST, true);
if ($postMultipart)
{
if (class_exists('\CURLFile')) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
} else {
if (defined('CURLOPT_SAFE_UPLOAD')) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
else
{
$header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8");
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
}
}
$reponse = curl_exec($ch);

if (curl_errno($ch))
{
throw new Exception(curl_error($ch),0);
}
else
{
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $httpStatusCode)
{
throw new Exception($reponse,$httpStatusCode);
}
}
curl_close($ch);
return $reponse;
}


 

---------------------------------------------------------------------------------------------
唯有志存高远,方能风行天下。

道之所存,虽千万人吾往矣! 情之所钟,虽千万里吾念矣~

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。