By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,353 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,353 IT Pros & Developers. It's quick & easy.

Connect through proxy?

P: 51
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?
Jul 25 '08 #1
Share this Question
Share on Google+
6 Replies


P: 51
Bump. Anybody? Sorry this is a kind of bizarre problem.
Jul 28 '08 #2

P: 5
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.

http://www.php.net/fopen

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

:-D
Jul 28 '08 #3

P: 51
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.net/jess/examples/php/pear/http_request/

I end up with:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. require_once 'HTTP/Request.php';
  3.  
  4. $params['proxy_host'] = '127.0.0.1'; //proxy is running on local host
  5. $params['proxy_port'] = 1080; //running on port 1080
  6. $webAddress = 'http://www.collicott.net/nickolas/';
  7.  
  8. $req =& new HTTP_Request($webAddress, $params);
  9. if (!PEAR::isError($req->sendRequest())) {
  10.     $webContent = $req->getResponseBody();
  11.     $webContent = preg_replace('/<!.*>|<\/?(html|head|meta|body).*>|<title>.*<\/title>/i','',$webContent); // strip tags
  12.     $webContent = preg_replace('/src="/i','src="'.$webAddress,$webContent); // fix images
  13.     $webContent = preg_replace('/href="/i','href="'.$webAddress,$webContent); // fix links
  14.     echo $webContent;
  15. }
  16. ?>
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?
Jul 28 '08 #4

P: 51
Bump again, sorry guys.
Jul 29 '08 #5

P: 51
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.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. require_once './HTTP/Request.php';
  3.  
  4. PEAR::setErrorHandling(PEAR_ERROR_PRINT);
  5.  
  6. $webAddress = 'http://www.yahoo.com';
  7.  
  8. $req =& new HTTP_Request($webAddress);
  9. $req->setProxy('127.0.0.1', 1080);
  10. if (PEAR::isError($req->sendRequest())) {
  11.     echo "ERROR!\n";
  12. }
  13. $req->sendRequest();
  14. $webContent = $req->getResponseBody();
  15. echo $webContent;
  16. ?>
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?
Jul 30 '08 #6

P: 51
Sorry, but I'm going to have to bump this one more time...
Aug 2 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.