doudanma9706 2016-04-11 09:17 采纳率: 100%
浏览 35
已采纳

cURL:Proxy Server获取目标图像

I need some help. In creating a simple proxy server using this curl function.

$ch = curl_init($IP);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: $HOSTNAME'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($ch);

it can redirects to the other server but it does change the links, like to target an image, what function should i need to add? thank you.

  • 写回答

1条回答 默认 最新

  • dousi8931 2016-04-11 09:21
    关注

    I think what you mean to say is if the final link has an img then it is not changing the url inside "src" attribute. That is not possible in curl. What you need to do for that is parse the html and the manually change the links so that they are directed from your proxy server.

    Also for creating a proxy server, you don't use host. You should fetch the contents using curl and print it out. What you are doing is a redirect and not a proxy

    Edit

    Sample code for the proxy

    <?php
        $host = "1.1.1.1"; //This is the host
        $url = $host.$_SERVER['REQUEST_URI'];
        echo file_get_contents($url);
        var_dump($http_response_header); //For debugging
    ?>
    

    Because you are only making http request i think file_get_contents is a better suited function. Also the var_dump will give you response headers from the "1.1.1.1" ip. Now a little note here. Have a look at content-type . This is important for images. Because browser needs to know the content type of the response. So what you need to do is parse the http_response_headers and then check for content-type and add headers in your response using header() function accordingly.

    Hope this helps you better

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序