Connect through proxy?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Muffinthief
    New Member
    • May 2007
    • 51

    #1

    Connect through proxy?

    Ok this question may seem odd, but I'll do my best to explain it.
    I'm writing a script for development purposes so this doesn't need to run in a browser, actually this would be a lot easier if it could, but it can't. I'm running it with:

    # /usr/local/bin/php script.php

    The script in question uses the Flickr API so it needs to connect to the internet. And here in lies the question. The way I'm connecting to the internet is through an ssh proxy. I run the command.

    # ssh -D 1080 mchang@192.168. 107.30

    Is there a way to tell the script to connect through that proxy?
  • Muffinthief
    New Member
    • May 2007
    • 51

    #2
    Bump. Anybody? Sorry this is a kind of bizarre problem.

    Comment

    • pcfreak30
      New Member
      • May 2008
      • 5

      #3
      luckly, the other day i was looking up the fopen function in the php manual. someone posted a comment with a proxy class. It allows to to fetch pages even if ur behind a proxy. The header commands are quite simple in my opinion. They are identical to talking to a web server.



      just scroll down trough the comments, and u will find it

      :-D

      Comment

      • Muffinthief
        New Member
        • May 2007
        • 51

        #4
        Ok guys thanks for the help. after a little more research I've tracked down my problem. The Flickr API (or at least what I'm using) uses PEAR. and you need to specify a proxy when making an HTTP request. Following the example from: http://www.collicott.n et/jess/examples/php/pear/http_request/

        I end up with:

        Code:
        <?php
        require_once 'HTTP/Request.php';
        
        $params['proxy_host'] = '127.0.0.1'; //proxy is running on local host
        $params['proxy_port'] = 1080; //running on port 1080
        $webAddress = 'http://www.collicott.net/nickolas/';
        
        $req =& new HTTP_Request($webAddress, $params);
        if (!PEAR::isError($req->sendRequest())) {
        	$webContent = $req->getResponseBody();
        	$webContent = preg_replace('/<!.*>|<\/?(html|head|meta|body).*>|<title>.*<\/title>/i','',$webContent); // strip tags
        	$webContent = preg_replace('/src="/i','src="'.$webAddress,$webContent); // fix images
        	$webContent = preg_replace('/href="/i','href="'.$webAddress,$webContent); // fix links
        	echo $webContent;
        }
        ?>
        This doesn't work, it runs as if I have no internet connection. My first post shows how I set up the proxy. Does anybody have any suggestions?

        Comment

        • Muffinthief
          New Member
          • May 2007
          • 51

          #5
          Bump again, sorry guys.

          Comment

          • Muffinthief
            New Member
            • May 2007
            • 51

            #6
            Ok guys well here's a simplified example of what I'm trying to do.

            I start the proxy using "ssh -D 1080 mchang@192.168. 107.30"

            this starts a proxy to another part of the machine (I'm using Solaris 10) on part 1080.

            Then I have the php code.
            Code:
            <?php
            require_once './HTTP/Request.php';
            
            PEAR::setErrorHandling(PEAR_ERROR_PRINT);
            
            $webAddress = 'http://www.yahoo.com';
            
            $req =& new HTTP_Request($webAddress);
            $req->setProxy('127.0.0.1', 1080);
            if (PEAR::isError($req->sendRequest())) {
            	echo "ERROR!\n";
            }
            $req->sendRequest();
            $webContent = $req->getResponseBody();
            echo $webContent;
            ?>
            Which should get yahoo.com and be all happy, but I get a "Malformed response" error. So I think either PEAR doesn't support Socks proxies or I'm doing something wrong. Does anybody have any ideas?

            Comment

            • Muffinthief
              New Member
              • May 2007
              • 51

              #7
              Sorry, but I'm going to have to bump this one more time...

              Comment

              Working...