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 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口