標籤:ror errno 返回 網站 16px cookies use pos err
一 需求
在A網站註冊一個新使用者,那麼,在B網站也會被同時註冊
二 思路
在A網站註冊的同時,調用API介面實現在B網站也會被同時註冊
三 實現
主要代碼如下:
1 function http_curl($url,$post=‘‘,$cookie=‘‘, $returnCookie=0){ 2 $curl = curl_init(); 3 curl_setopt($curl, CURLOPT_URL, $url); 4 curl_setopt($curl, CURLOPT_USERAGENT, ‘Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)‘); 5 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 6 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); 7 curl_setopt($curl, CURLOPT_REFERER, "http://XXX"); 8 if($post) { 9 curl_setopt($curl, CURLOPT_POST, 1);10 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));11 }12 if($cookie) {13 curl_setopt($curl, CURLOPT_COOKIE, $cookie);14 }15 curl_setopt($curl, CURLOPT_HEADER, $returnCookie);16 curl_setopt($curl, CURLOPT_TIMEOUT, 10);17 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);18 $data = curl_exec($curl);19 if (curl_errno($curl)) {20 return curl_error($curl);21 }22 curl_close($curl);23 if($returnCookie){24 list($header, $body) = explode("\r\n\r\n", $data, 2);25 preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);26 $info[‘cookie‘] = substr($matches[1][0], 1);27 $info[‘content‘] = $body;28 return $info;29 }else{30 return $data;31 }32 }View Code
參數說明:參數1為訪問的URL,參數2為post資料(不填則為GET),參數3為提交的$cookies,參數4為是否返回$cookies。
使用 curl() 函數實現不同網站之間註冊使用者的同步