The content of this article is about how PHP uses cURL to call WebService to obtain weather information (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The relevant codes are as follows:
<?php $data = 'theCityName=杭州'; $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"); curl_setopt($curl,CURLOPT_HEADER,0); curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); curl_setopt($curl,CURLOPT_POST,1); curl_setopt($curl,CURLOPT_POSTFIELDS,$data); curl_setopt($curl,CURLOPT_HTTPHEADER,array( "application/x-www-form-urlencoded; charset=utf-8", "Content-length: ".strlen($data) )); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'); $result = curl_exec($curl); if (!curl_errno($curl)) { echo $result; } else { echo 'Curl error:' . curl_error($curl); } curl_close($curl);
Related recommendations:
How to use TP5.1 template loop tags (code)
php implementation to generate mixed verification code and image verification code and test (code)
The above is the detailed content of How does php use cURL to call WebService to obtain weather information (code). For more information, please follow other related articles on the PHP Chinese website!