Home > Article > Backend Development > PHP curl_init sets HTTP server authentication
When using PHP’s cURL library to crawl web pages, sometimes the HTTP server requires authentication. How should I set it up?
<?php $url = "http://192.168.0.100:8080/JM-PLATFORM/sms/MobsetSendSMS/sysId/oa/mobileNum/{$mobile_phone}/message/{$message}"; $tmp = sys_get_temp_dir(); $cookieDump = tempnam($tmp, 'cookies'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设置存放 Cookie 的文件 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieDump); //设置 Http 身份验证的方法,这里有多个选项可以选择,可参考 php 手册 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //设置 用户名和密码 curl_setopt($ch, CURLOPT_USERPWD,'User:Password'); $output = curl_exec($ch); curl_close($ch); echo $output; ?>