Yii2.0 - CURL
1 <?php 2 namespace app\controllers; 3 4 use linslin\yii2\curl\Curl; 5 6 class WasteAllArrivalController extends BaseController 7 { 8 protected $url ='http://www.purchase.com/v1/alibaba/get-logistics/system.oauth2/'; 9 10 /** 11 * === Yii2.0 自带 === 12 */ 13 public function actionYiiCurl($action = 'postponeToken/') 14 { 15 //$url = 'http://www.purchase.com/v1/alibaba/get-logistics'; 16 //$client_id = urlencode($account->app_key); 17 18 $username = Yii::$app->request->get('username'); 19 $password = 'mima'; 20 21 $curl = new curl\Curl(); //$curl = new Curl(); 22 $res = $curl->setPostParams([ 23 'username' => $username, 24 'password' => $password, 25 ])->post($this->url . $action); 26 //$data = json_decode($res,TRUE); 27 $s = Json::decode($res); //$s['access_token'] 28 } 29 30 /** 31 * === 原生 === 32 */ 33 public function actionCurl() 34 { 35 $username = trim(\Yii::$app->request->post('bind_account')); 36 //创建一个新cURL资源 37 $ch = curl_init(); 38 // 设置URL和相应的选项 39 curl_setopt($ch, CURLOPT_URL, "http://www.purchase.com/v1/alibaba/get-logistics?account=".$bind_account); 40 curl_setopt($ch, CURLOPT_HEADER, 0); 41 // 抓取URL并把它传递给浏览器 42 $data = curl_exec($ch); 43 //关闭cURL资源,并且释放系统资源 44 curl_close($ch); 45 } 46 }