当前位置:首页 > PHP > 正文内容

php curl指定ip,php curl请求忽略本地host文件,php curl请求跳过本地host文件

高老师6年前 (2020-08-17)PHP1649

假如我们使用curl请求一个网站,如果这个网站域名在本地host中也存在,curl默认会请求本地,但是我们可以自己设置解析到哪个ip。

(1).设置朋友的博客网站主机和ip,请求测试正确返回远程网站内容

<?php

$ip = '47.106.110.119';
$host = 'www.php20.cn';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ip);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

$headers = array();
$headers[] = 'Host:'.$host;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
file_put_contents('1.txt', $result);
var_dump($result);

//输出<!DOCTYPE html><html><head><meta charset="UTF-8"><title>仙士可博客,技术博客,php,技术分享,php博客,</title>

(2).设置请求ip为本地,输出本地网站的内容

<?php

$ip = '127.0.0.1';
$host = 'www.php20.cn';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ip);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

$headers = array();
$headers[] = 'Host:'.$host;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
file_put_contents('1.txt', $result);
var_dump($result);

//输出本地站点

未测试https

也可以试试百度的方法

curl_setopt($curl, CURLOPT_INTERFACE, 'ip');

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/149.html

分享给朋友:

“php curl指定ip,php curl请求忽略本地host文件,php curl请求跳过本地host文件” 的相关文章

php将html转为pdf,php将html页面导出pdf

php将html转为pdf,php将html页面导出pdf

首先下载wkhtmltox-0.12.4_linux-generic-amd64.tar.xz   (不要下载RPM包,依赖太多,需要x-server支持),并解压,执行测试运行正常tar wkhtmltox-0.12.4_linux-generic-amd64.tar.xzcd...

php调用 java webservice接口

php调用 java webservice接口

php调用Webservice基本语法如下:$url ='xxxxxxx.cn'  //链接服务器端 $client = new SoapClient($url);通过以上语法已经连接到webservice,也可将wsdl在本地使用,...

Thinkphp Call Stack,PHP调用栈Call Stack的获取

Thinkphp Call Stack,PHP调用栈Call Stack的获取

ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。<?php //--框架核心--Start //框架内置错误处理 function errDealWith($er...

php通过event扩展创建定时器,php毫秒级定时器

php通过event扩展创建定时器,php毫秒级定时器

PHP简单定时器可以通过pcntl_signal创建闹钟信号来实现。但是缺点很明显,性能一般,要自己实现守护进程,不支持毫秒级定时器,单进程不支持多个闹钟信号,不能跨平台运行event扩展支持的事件多,性能高。<?php //创建event配置.[空配置] $eventConfig ...

windows安装php event扩展问题

windows安装php event扩展问题

php event扩展在windows中依赖于php_sockets扩展,因此在php.ini中必须先加载php sockets扩展,如下。extension=sockets extension=event...

php管道通信

php管道通信

(1).管道是干嘛的?管道是用于进程之间通信的,传播或交换信息(2).管道有几种?(2.1).匿名管道(pipe):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用。通常是指父子进程关系。(2.2).高级管道(popen):将另一个程序当做一个新的进程在当前程序进程中...