thinkphp5ÍøÂçÇëÇó
ÔÚweb¿ª·¢Àú³ÌÖУ¬ÍøÂçÇëÇóÊÎÑÝןÜÊÇÖ÷ÒªµÄ½ÇÉ«¡£ÌØÊâÊÇÔÚ»ùÓÚphpÓïÑÔ¾ÙÐÐweb¿ª·¢µÄÀú³ÌÖУ¬ÍøÂçÇëÇóµÄ´¦ÀíÊDZر¸µÄÊÖÒÕÖ®Ò»¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphp5¿ò¼ÜÖоÙÐÐÍøÂçÇëÇóµÄ´¦Àí¡£
Ò»¡¢ÇëÇó·½·¨
ThinkPHP5¿ò¼ÜÖ§³ÖËùÓÐHTTPÇëÇóÒªÁ죬ÆäÖаüÀ¨GET¡¢POST¡¢PUT¡¢DELETEµÈ¡£³£¼ûµÄÇëÇó·½·¨ÓÐÁ½ÖÖ£¬Ò»ÖÖÊÇʹÓÃPHPÔÉúµÄCURL¿â¾ÙÐÐÇëÇó£¬ÁíÒ»ÖÖÊÇʹÓÃThinkPHP5¿ò¼ÜÄÚÖõÄHTTPÀà¿â¾ÙÐÐÇëÇó¡£
1.ʹÓÃÔÉúCURL¿â¾ÙÐÐÇëÇó
ʹÓÃCURL¿â¾ÙÐÐÍøÂçÇëÇóÊǷǾ³£¼ûÇÒÊÊÓõÄÒªÁ죬¿ÉÒÔÎÞаµØ¿ØÖÆÇëÇóÀú³Ì£¬Í¬Ê±Ò²ÄÜ»ñÈ¡ÇëÇóµÄЧ¹û¡£ÒÔÏÂÊÇÒ»¸öʹÓÃÔÉúCURL¿â¾ÙÐÐGETÇëÇóµÄ´úÂëʾÀý£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
$url = 'http://www.example.com'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); echo $output;
µÇ¼ºó¸´ÖÆ
¸ÃʾÀýÖУ¬Í¨¹ýcurl_init()º¯ÊýÀ´³õʼ»¯CURL¾ä±ú£¬È»ºóͨ¹ýcurl_setopt()º¯ÊýÀ´ÉèÖÃCURL¾ä±úµÄÑ¡Ï×îºóͨ¹ýcurl_exec()º¯ÊýÀ´Ö´ÐÐÇëÇ󣬻ñÈ¡ÇëÇóЧ¹û¡£Íê³ÉÇëÇóÖ®ºó£¬Í¨¹ýcurl_close()º¯ÊýÀ´¹Ø±ÕCURL¾ä±ú¡£
ʹÓÃThinkPHP5¿ò¼ÜÄÚÖÃHTTPÀà¿â¾ÙÐÐÇëÇó
ThinkPHP5¿ò¼ÜÄÚÖÃHTTPÀà¿â£¬¿ÉÒÔ×ÊÖúÎÒÃǸüÀû±ãµØ¾ÙÐÐÍøÂçÇëÇóµÄ´¦Àí£¬ÁíÍâÒ²ÔöÌíÁ˶ÔHTTPSÐÒéµÄÖ§³Ö¡£ÒÔÏÂÊÇÒ»¸öʹÓÃHTTPÀà¿â¾ÙÐÐGETÇëÇóµÄ´úÂëʾÀý£º
use thinkacadeHttp; $url = 'http://www.example.com'; $response = Http::get($url); echo $response->getBody();
µÇ¼ºó¸´ÖÆ
¸ÃʾÀýÖУ¬Ê¹ÓÃÁË¿ò¼ÜÄÚÖõÄHttpÀà¿â¾ÙÐÐGETÇëÇó¡£Http::get()ÒªÁìÎüÊÕÒ»¸öURL²ÎÊý£¬²¢·µ»ØÒ»¸öÏìÓ¦¹¤¾ß£¬Í¨¹ýgetBody()ÒªÁì»ñÈ¡ÏìÓ¦µÄÄÚÈÝ¡£
¶þ¡¢ÇëÇó²ÎÊý
ÔÚ¾ÙÐÐÍøÂçÇëÇóʱ£¬ÓÐʱÐèҪת´ï²ÎÊýÐÅÏ¢¡£ÒÔÏÂÊÇPOSTÇëÇóµÄ²ÎÊýת´ï·½·¨¡£
ʹÓÃÔÉúCURL¿â¾ÙÐÐPOSTÇëÇó
ʹÓÃÔÉúCURL¿â¾ÙÐÐPOSTÇëÇóʱ£¬ÐèҪʹÓÃcurl_setopt()º¯ÊýÉèÖÃCURLOPT_POSTÑ¡Ïͬʱת´ï²ÎÊýÐÅÏ¢¡£ÒÔÏÂÊÇÒ»¸öʹÓÃÔÉúCURL¿â¾ÙÐÐPOSTÇëÇóµÄ´úÂëʾÀý£º
$url = 'http://www.example.com'; $data = array('name' => 'John Doe', 'email' => 'johndoe@example.com'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); echo $output;
µÇ¼ºó¸´ÖÆ
¸ÃʾÀýÖУ¬Ê¹ÓÃÁËcurl_setopt()º¯ÊýÉèÖÃCURLOPT_POSTÑ¡ÏÆôPOSTÇëÇ󣬲¢Í¨¹ýCURLOPT_POSTFIELDSÑ¡Ïîת´ï²ÎÊýÐÅÏ¢¡£
2.ʹÓÃThinkPHP5¿ò¼ÜÄÚÖÃHTTPÀà¿â¾ÙÐÐPOSTÇëÇó
ʹÓÃHTTPÀà¿â¾ÙÐÐPOSTÇëÇóʱ£¬ÐèҪͨ¹ý$post²ÎÊýת´ï²ÎÊýÐÅÏ¢¡£ÒÔÏÂÊÇÒ»¸öʹÓÃHTTPÀà¿â¾ÙÐÐPOSTÇëÇóµÄ´úÂëʾÀý£º
use thinkacadeHttp; $url = 'http://www.example.com'; $data = array('name' => 'John Doe', 'email' => 'johndoe@example.com'); $response = Http::post($url, $data); echo $response->getBody();
µÇ¼ºó¸´ÖÆ
¸ÃʾÀýÖУ¬Ê¹ÓÃÁËHttp::post()ÒªÁ죬²¢½«²ÎÊýÐÅϢͨ¹ý$data²ÎÊýת´ï¡£
Èý¡¢ÏìÓ¦´¦Àí
´¦ÀíÍøÂçÇëÇóʱ£¬ÐèÒª»ñÈ¡ÇëÇóµÄЧ¹û£¬ÒÔ±ãÓÚ´¦Àí¡£ÒÔÏÂÊÇÍøÂçÇëÇóЧ¹û´¦ÀíµÄ·½·¨¡£
1.ʹÓÃCURL¿â¾ÙÐÐÏìÓ¦´¦Àí
ʹÓÃCURL¿â¾ÙÐÐÏìÓ¦´¦Àíʱ£¬ÐèҪͨ¹ýcurl_exec()º¯Êý»ñÈ¡ÇëÇóµÄЧ¹û£¬È»ºóÆÊÎöЧ¹û¾ÙÐÐÓªÒµÂß¼´¦Àí¡£ÒÔÏÂÊÇÒ»¸öʹÓÃCURL¿â¾ÙÐÐÏìÓ¦´¦ÀíµÄ´úÂëʾÀý£º
$url = 'http://www.example.com'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $result = json_decode($output, true); echo $result['data']['name'];
µÇ¼ºó¸´ÖÆ
¸ÃʾÀýÖУ¬Ê¹ÓÃcurl_exec()º¯Êý»ñÈ¡ÇëÇóµÄЧ¹û£¬²¢Ê¹ÓÃjson_decode()º¯ÊýÆÊÎöЧ¹û¡£
2.ʹÓÃThinkPHP5¿ò¼ÜÄÚÖÃHTTPÀà¿â¾ÙÐÐÏìÓ¦´¦Àí
ʹÓÃHTTPÀà¿â¾ÙÐÐÏìÓ¦´¦Àíʱ£¬¿ÉÒÔͨ¹ýÏìÓ¦¹¤¾ß¾ÙÐвÙ×÷£¬Ò²¿ÉÖ±½Ó»ñÈ¡ÏìÓ¦µÄÄÚÈÝ¡£ÒÔÏÂÊÇÒ»¸öʹÓÃHTTPÀà¿â¾ÙÐÐÏìÓ¦´¦ÀíµÄ´úÂëʾÀý£º
use thinkacadeHttp; $url = 'http://www.example.com'; $response = Http::get($url); $result = $response->json(); echo $result['data']['name'];
µÇ¼ºó¸´ÖÆ
¸ÃʾÀýÖУ¬Ê¹ÓÃHttp::get()ÒªÁì»ñÈ¡ÇëÇóµÄЧ¹û£¬²¢Í¨¹ý$response->json()ÒªÁ콫Ч¹ûÆÊÎöΪÊý×é¡£
ËÄ¡¢¹ýʧ´¦Àí
ÔÚ¾ÙÐÐÍøÂçÇëÇóµÄÀú³ÌÖУ¬ÓпÉÄ᷺ܻÆðÍøÂçÒì³£»òЧÀÍÆ÷¹ýʧµÈÎÊÌ⣬Òò´ËÐèÒª¾ÙÐйýʧ´¦Àí¡£ÒÔÏÂÊÇÍøÂçÇëÇóʱ³£¼ûµÄ¹ýʧ´¦Àí·½·¨¡£
1.ʹÓÃCURL¿â¾ÙÐйýʧ´¦Àí
ʹÓÃCURL¿â¾ÙÐйýʧ´¦Àíʱ£¬ÐèÒª¼ì²âÇëÇóµÄ·µ»ØÖµ£¬ÒÔÈ·¶¨ÊÇ·ñ·ºÆðÁ˹ýʧ¡£ÒÔÏÂÊÇÒ»¸öʹÓÃCURL¿â¾ÙÐйýʧ´¦ÀíµÄ´úÂëʾÀý£º
$url = 'http://www.example.com'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); if (curl_errno($ch)) { echo 'Error: ' . curl_error($ch); } else { $result = json_decode($output, true); echo $result['data']['name']; } curl_close($ch);
µÇ¼ºó¸´ÖÆ
¸ÃʾÀýÖУ¬Ê¹ÓÃcurl_errno()º¯Êý»ñÈ¡CURLÖ´ÐÐÀú³ÌÖеĹýʧ´úÂ룬ͬʱʹÓÃcurl_error()º¯Êý»ñÈ¡¹ýʧÐÎò¡£
2.ʹÓÃThinkPHP5¿ò¼ÜÄÚÖÃHTTPÀà¿â¾ÙÐйýʧ´¦Àí
ʹÓÃHTTPÀà¿â¾ÙÐйýʧ´¦Àíʱ£¬ÐèҪͨ¹ýtry…catchÓï¾ä²¶»ñÒì³£¡£ÒÔÏÂÊÇÒ»¸öʹÓÃHTTPÀà¿â¾ÙÐйýʧ´¦ÀíµÄ´úÂëʾÀý£º
use thinkacadeHttp; $url = 'http://www.example.com'; try { $response = Http::get($url); $result = $response->json(); echo $result['data']['name']; } catch (Exception $e) { echo $e->getMessage(); }
µÇ¼ºó¸´ÖÆ
¸ÃʾÀýÖУ¬Ê¹ÓÃtry…catchÓï¾ä²¶»ñHTTPÇëÇóÀú³ÌÖеÄÒì³££¬²¢Ê¹ÓÃgetMessage()ÒªÁì»ñÈ¡Òì³£ÐÅÏ¢¡£
×ܽá
ÍøÂçÇëÇóÊÇWeb¿ª·¢Àú³ÌÖв»¿É»òȱµÄÒ»»·£¬Ñ§»á׼ȷ´¦ÀíÍøÂçÇëÇóÊÇÿ¸öPHP Web¿ª·¢¹¤³Ìʦ±Ø±¸µÄÊÖÒÕÖ®Ò»¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑùÔÚThinkPHP5¿ò¼ÜÖоÙÐÐÍøÂçÇëÇóµÄ´¦Àí£¬°üÀ¨ÇëÇó·½·¨¡¢ÇëÇó²ÎÊý¡¢ÏìÓ¦´¦ÀíºÍ¹ýʧ´¦ÀíµÈ·½Ã档ϣÍû¶ÔÄúÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇthinkphp5ÍøÂçÇëÇóµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡