停止卷曲以降低端口号

问题描述

| 当我卷曲以下
<?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也是问题的一部分。     

解决方法

        除非您要构建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)
  {
  }