×ðÁú¿­Ê±

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ÍøÂçÇëÇóµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿