Home >Backend Development >PHP Tutorial >PHP curl_init sets HTTP server authentication

PHP curl_init sets HTTP server authentication

巴扎黑
巴扎黑Original
2016-11-09 11:19:571085browse

When using PHP’s cURL library to crawl web pages, sometimes the HTTP server requires authentication. How should I set it up?

PHP curl_init sets HTTP server authentication

<?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, &#39;cookies&#39;);
    $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,&#39;User:Password&#39;);
    $output = curl_exec($ch);
    curl_close($ch);
    echo $output;
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn