200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > PHP爬取post网页数据 php curl发送post请求爬取webService接口数据简单实例

PHP爬取post网页数据 php curl发送post请求爬取webService接口数据简单实例

时间:2023-05-07 00:33:54

相关推荐

PHP爬取post网页数据 php curl发送post请求爬取webService接口数据简单实例

header('Content-Type: text/html;charset=utf-8');

/**

* 使用curl发送post请求

* @param $url

* @param string $data

* @return bool|mixed

*/

function sendCurlPost($url, $data = ''){

//初始化,创建一个cURL资源

$ch = curl_init();

//设置cURL选项

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_USERAGENT, "user-agent:Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/0101 Firefox/24.0");

curl_setopt($ch, CURLOPT_HEADER, 0); //是否返回文件头信息

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不直接打印输出

curl_setopt($ch, CURLOPT_POST, 1); //是否post请求

curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //post传输数据

curl_setopt($ch, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8", "Content-length: ".strlen($data)));

//执行cURL会话

$response = curl_exec($ch);

if (!curl_errno($ch)){

$result = $response;

}else{

// echo 'Curl error: ' . curl_error($ch);

$result = false;

}

//关闭cURL释放资源

curl_close($ch);

return $result;

}

$url = "/WebServices/WeatherWebService.asmx/getWeatherbyCityName";

$data = 'theCityName=上海';

//cURL调用webservice爬取上海市天气数据

echo sendCurlPost($url, $data);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。