停止curl删除端口号

发布于 2024-11-11 20:49:52 字数 560 浏览 3 评论 0原文

当我卷曲以下页面时,

<?php

$ch = curl_init();
curl_setopt ($ch, CURLOPT_PORT, "8081");
curl_setopt ($ch, CURLOPT_URL, "http://192.168.0.14:8081/comingEpisodes/" );
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($ch);
curl_close($ch);

echo $curl_response;
?>

返回了页面,但图像没有返回。我找到了问题所在。 192.168.0.14 是我的本地主机。我正在从运行端口 8081 的应用程序调用页面。Curl 似乎删除了该端口并将 192.168.0.14 更改为 locahost,因此图像不再链接到正确的位置。如何确保端口保留以便图像保留。谢谢

编辑:我认为端口之后的 /comingEpisodes 也是问题的一部分。

When I curl the following

<?php

$ch = curl_init();
curl_setopt ($ch, CURLOPT_PORT, "8081");
curl_setopt ($ch, CURLOPT_URL, "http://192.168.0.14:8081/comingEpisodes/" );
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($ch);
curl_close($ch);

echo $curl_response;
?>

The page is returned however the images aren't. I located the problem. 192.168.0.14 is my local host. I am calling a page from an app the runs off port 8081. Curl seems to drop the port and change 192.168.0.14 to locahost and therefore the images are no longer linked to the right place. How do I make sure that the port remains so the images remain. Thanks

EDIT: I think the /comingEpisodes after the port is also part of the problem..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛徒 2024-11-18 20:49:53

除非您正在构建 100% 代理,否则您会将 cURL 拉取的内容转储到浏览器中。现在,结果来自 cURL 结果转储到的页面,而不是来自原始 cURL 请求。

基本上,如果您访问 http://localhost 并且上述代码位于 index.php 中,那么页面正在请求 :8081/comingEpisodes 内容并将其转储到原始 http://locahost上下文中>。浏览器现在基于 http://localhost 找到的所有内容,而不是像来自curl 请求一样。

可以在将文档输出到某个“proxy.php?retrieve=old_url”之前替换文档中的所有内容链接,然后让所有这些现在通过相同的 cURL 上下文进行调用,但这是网络代理。

End-User               Intermediary              End-Website
(http://localhost)     (localhost/index.php)     (http://192.168.0.14:8081/comingEpisodes/)
------------------     ---------------------     ------------------------------------------
Initial visit--------->
                       cURL Request------------->
                                                 Page Content (html basically)
                       Echoed back to user<------
Content<---------------
Finds <img> etc.------>
                       /comingEpisodes/img1.jpg  // 404 error, it's actually on :8081
                                                 // that localhost has no idea about
                                                 // because it's being hidden using cURL

非常简单的演示

<?php
  //
  // Very Dummied-down proxy
  //

  // Either get the url of the content they need, or use the default "page root"
  // when none is supplied. This is not robust at all, as this really only handles
  // relative urls (e.g. src="images/foo.jpg", something like src="http://foo.com/"
  // would become src="index.php?proxy=http://foo.com/" which makes the below turn
  // into "http://www.google.com/http://foo.com/")
  $_target = 'http://www.google.com/' . (isset($_GET['proxy']) ? $_GET['proxy'] : '');

  // Build the cURL request to get the page contents
  $cURL = curl_init($_target);
  try
  {
    // setup cURL to your liking
    curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);

    // execute the request
    $page = curl_exec($cURL);

    // Forward along the content type (so images, files, etc all are understood correctly)
    $contentType = curl_getinfo($cURL, CURLINFO_CONTENT_TYPE);
    header('Content-Type: ' . $contentType);

    // close curl, we're done.
    curl_close($cURL);

    // test against the content type. If it HTML then we need to re-parse
    // the page to add our proxy intercept in the URL so the visitor keeps using
    // our cURL request above for EVEYRTHING it needs from this site.
    if (strstr($contentType,'text/html') !== false)
    {
      //
      // It's html, replace all the references to content using URLs
      //

      // First, load our DOM parser
      $html = new DOMDocument();
      $html->formatOutput = true;
      @$html->loadHTML($page); // was getting parse errors, added @ for demo purposes.

      // simple demo, look for image references and change them
      foreach ($html->getElementsByTagName('img') as $img)
      {
        // take a typical image:
        //   <img src="logo.jpg" />
        // and make it go through the proxy (so it uses cURL again:
        //   <img src="index.php?proxy=logo.jpg" />
        $img->setAttribute('src', sprintf('%s?proxy=%s', $_SERVER['PHP_SELF'], urlencode($img->getAttribute('src'))));
      }

      // finally dump it to client with the urls changed
      echo $html->saveHTML();
    }
    else
    {
      // Not HTML, just dump it.
      echo $page;
    }
  }
  // just in case, probably want to do something with this.
  catch (Exception $ex)
  {
  }

Unless you're building a 100% proxy, you're dumping the contents of the cURL pull in to a browser. The results now refer from the page that the cURL results are dumped to, not from the originating cURL request.

Basically, if you visit http://localhost and the above code resides in the index.php, that page is requesting the :8081/comingEpisodes contents and dumping it within the context of the originating http://locahost. The browser is now basing all the found content from http://localhost and not as if it were from the curl request.

You could replace all the content links within the document before it's output to some "proxy.php?retrieve=old_url" and then make all those now call through the same cURL context, but that's the basis of a web proxy.

End-User               Intermediary              End-Website
(http://localhost)     (localhost/index.php)     (http://192.168.0.14:8081/comingEpisodes/)
------------------     ---------------------     ------------------------------------------
Initial visit--------->
                       cURL Request------------->
                                                 Page Content (html basically)
                       Echoed back to user<------
Content<---------------
Finds <img> etc.------>
                       /comingEpisodes/img1.jpg  // 404 error, it's actually on :8081
                                                 // that localhost has no idea about
                                                 // because it's being hidden using cURL

VERY SIMPLE DEMO

<?php
  //
  // Very Dummied-down proxy
  //

  // Either get the url of the content they need, or use the default "page root"
  // when none is supplied. This is not robust at all, as this really only handles
  // relative urls (e.g. src="images/foo.jpg", something like src="http://foo.com/"
  // would become src="index.php?proxy=http://foo.com/" which makes the below turn
  // into "http://www.google.com/http://foo.com/")
  $_target = 'http://www.google.com/' . (isset($_GET['proxy']) ? $_GET['proxy'] : '');

  // Build the cURL request to get the page contents
  $cURL = curl_init($_target);
  try
  {
    // setup cURL to your liking
    curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);

    // execute the request
    $page = curl_exec($cURL);

    // Forward along the content type (so images, files, etc all are understood correctly)
    $contentType = curl_getinfo($cURL, CURLINFO_CONTENT_TYPE);
    header('Content-Type: ' . $contentType);

    // close curl, we're done.
    curl_close($cURL);

    // test against the content type. If it HTML then we need to re-parse
    // the page to add our proxy intercept in the URL so the visitor keeps using
    // our cURL request above for EVEYRTHING it needs from this site.
    if (strstr($contentType,'text/html') !== false)
    {
      //
      // It's html, replace all the references to content using URLs
      //

      // First, load our DOM parser
      $html = new DOMDocument();
      $html->formatOutput = true;
      @$html->loadHTML($page); // was getting parse errors, added @ for demo purposes.

      // simple demo, look for image references and change them
      foreach ($html->getElementsByTagName('img') as $img)
      {
        // take a typical image:
        //   <img src="logo.jpg" />
        // and make it go through the proxy (so it uses cURL again:
        //   <img src="index.php?proxy=logo.jpg" />
        $img->setAttribute('src', sprintf('%s?proxy=%s', $_SERVER['PHP_SELF'], urlencode($img->getAttribute('src'))));
      }

      // finally dump it to client with the urls changed
      echo $html->saveHTML();
    }
    else
    {
      // Not HTML, just dump it.
      echo $page;
    }
  }
  // just in case, probably want to do something with this.
  catch (Exception $ex)
  {
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文