PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Can HTTP_Request2 handle a URL with a query_string?

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTTP
View previous topic :: View next topic  
Author Message
griffmcc



Joined: 05 Mar 2009
Posts: 7

PostPosted: Tue Sep 22, 2009 11:59 pm    Post subject: Can HTTP_Request2 handle a URL with a query_string? Reply with quote

I want to use HTTP_Request2 to read from URLs like:
http://finance.yahoo.com/q?s=SPY

The code works if I leave off the query string "q?s=SPY", but if I include the query string, I get the error: 400 Bad Request.

The problem is not with finance.yahoo.com because I have a python program that reads from the same URLs that are rejected with PHP.

What do I need to do to get HTTP_Request2 to handle the query string?

PHP code with error:

Code:
<?php
require_once('HTTP/Request2.php');

$r = new HTTP_Request2("http://finance.yahoo.com/q?s=SPY");
// $r = new HTTP_Request2('http://finance.yahoo.com'); THIS WORKS
$r->setMethod(HTTP_REQUEST_METHOD_GET);

$resp = $r->send();

$response_body = $resp->getBody();
print "Body: <br />\n";
print $response_body;
?>


Python program that works:

Code:
import urllib
import locale

def GetUrl(url):
    try:
        sock = urllib.urlopen(url)
        html = sock.read()
        sock.close()
        return html
    except:
        print "%s is not on yahoo" % url
        return

if __name__ == "__main__":

    print GetUrl('http://finance.yahoo.com/q?s=SPY')
Back to top
View user's profile Send private message
griffmcc



Joined: 05 Mar 2009
Posts: 7

PostPosted: Thu Sep 24, 2009 10:18 pm    Post subject: urlencode() and rawurlencode() didn't help Reply with quote

I tried to use urlencode() and rawurlencode() on either the entire URL or just the query string, but nothing worked.

Code:
$r = new HTTP_Request2("http://finance.yahoo.com/".rawurlencode("q?s=SPY")); // "Method not implemented"
// $r = new HTTP_Request2("http://finance.yahoo.com/".urlencode("q?s=SPY")); // "Method not implemented"
// $r = new HTTP_Request2("http://finance.yahoo.com/".urlencode("q?s='SPY'")); // "Method not implemented"
// $r = new HTTP_Request2(urlencode('http://finance.yahoo.com')); // "thrown in /usr/share/php/HTTP/Request2.php on line 779"
// $r = new HTTP_Request2("http://finance.yahoo.com/q?s='SPY'"); // 400 Bad Request
// $r = new HTTP_Request2('http://finance.yahoo.com/q?s=MXB'); // 400 Bad Request
// $r = new HTTP_Request2('http://finance.yahoo.com'); // WORKS
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTTP All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement