Creating a http request via PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bsharp42
Forum Newbie
Posts: 6
Joined: Sun Aug 02, 2009 12:30 am

Creating a http request via PHP

Post by bsharp42 »

Hello,

New to PHP and New to the forum here.

Anyway, for my website, I need to make a HTTPRequest to a websiteURL and get the response which will be an XML document (90% of the time).

I looked into the HTTPRequest class but I couldn't get it to work with my code.


echo 'test';
$r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
echo 'test';
$r->setOptions(array('cookies' => array('lang' => 'de')));
$r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
try {
echo $r->send()->getBody();
} catch (HttpException $ex) {
echo $ex;



This is what I have for my php.ini file....

#Sun Apr 05 14:04:18 IDT 2009
zend_extension=./ZendDebugger.so
extension=php_http.dll


I was told to add extension = php_http.dll to the ini file so I did, but its not working.
As you can see, I have put two "echo 'test';" phrases and the second echo doesn't actually get called so I'm guessing theres something wrong with the
httprequest code (probably not recognizing). I am not familiar with how import/include works with php..... I'm guessing php.ini is doing it ?

Anyway, help will be very much appreciated
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Creating a http request via PHP

Post by requinix »

Forget that thing - cURL is much easier to use, doesn't require any magic strings or option names, and is more widely used and supported.

Code: Select all

$ch = curl_init("http://www.example.com/form.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    "user" => "username",
    "pass" => "password"
));
// do you really need cookies for this?
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
$response = curl_exec($ch);
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Creating a http request via PHP

Post by Eran »

The HTTPRequest class is actually pretty decent. Certainly easier to use than cURL (cURL is for more advanced usage)
zend_extension=./ZendDebugger.so
extension=php_http.dll
You have a UNIX and a Windows extension here. Which are you running? if it's Linux, then you need to change .dll to .so
Don't forget to restart your apache service
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Creating a http request via PHP

Post by requinix »

pytrin wrote:The HTTPRequest class is actually pretty decent. Certainly easier to use than cURL (cURL is for more advanced usage)
Yeah... so I looked into it (after I posted, of course) and it does look rather nice. Not as powerful as cURL but since it can focus on one area in particular it can do it a bit better.
I really need to stop doing things like that... I think it's time for another hiatus from DevNetwork.
Post Reply