php curl 跨域请求例子

php curl 跨域请求例子

public function getresapi($url)
{
	 $ch = curl_init();
	 curl_setopt($ch, CURLOPT_URL, $url);
	 curl_setopt($ch, CURLOPT_HEADER, false);
	 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	 $result=curl_exec($ch);
	 curl_close($ch);
	 return($result);
}

$url = 'http://xxxx.com/index.php?r=api/xxx&from='.$starttime.'&to='.$endtime.'&url='.$url;
$result = $getresapi($url);


例子二

curl.php

<?php
$arrPostInfo  = array("key"=>11,"cyid"=>22,"startday"=>33,"endday"=>44,"page"=>55,"limit"=>66);
$url = 'http://test.xxx.com/curl_dome.php';//请求的url地址

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arrPostInfo);
$response  = curl_exec($ch);
curl_close($ch);
$result = json_decode($response,true);

var_dump($result);



curl_dome.php

<?php
$arrPostInfo  = array("key"=>11,"cyid"=>22,"startday"=>33,"endday"=>44,"page"=>55,"limit"=>66);
echo json_encode($arrPostInfo);



你可能感兴趣的:(php curl 跨域请求例子)