LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP Web Interactions (https://www.linuxquestions.org/questions/programming-9/php-web-interactions-946521/)

Tyler_H72 05-23-2012 04:05 PM

PHP Web Interactions
 
I'm having trouble with a PHP API that my company paid to access. We were given the specifications as far as what requests give which response and such, but we have no access to the authentication code. We can access the information by physically typing the requests into a browser, but I am having trouble thinking of a way to do this from a new web site. I know this is a very open question, but as I have little experience in web programming, does anyone have any suggestions?

eantoranz 05-23-2012 04:32 PM

I guess you want to make one HTTP request from a running PHP script (so that you can access a remote API as you are saying).

http://pear.php.net/HTTP_Request2

Tyler_H72 05-24-2012 08:56 AM

I have tried this with HttpRequest with no results- will it make a difference to try it with HTTP_Request2? Also, is the code I used correct? (As I said, I'm very new to web coding)


Code:

<html>
<body>
test
<?php

$req = new HttpRequest("<siteURL>", HttpRequest::METH_GET);
$req->send();

echo $req->getResponseBody();

?>
</body>
</html>


EDIT: I should probably mention that I am fairly certain this code is incorrect, as it appears to be breaking when I create the request, rather than when I send it.

eantoranz 05-24-2012 09:04 AM

What API is that? I mean, request class for HTTP/Request2 is HTTP_Request2? Also, so it doesn't work because? do you have logs? What's the possible return value of send()? Will it say if the request failed? If so, where can you get information about the failure reason?

In HTTP_Response2 it's something like:

Code:

$req = new HTTP_Request2();
$req->setUrl("$whateverurl");
try{
        $response = $req->send();
        $responseCode = $response->getStatus();
        if ($responseCode != 200) {
                // whatever
        } else {
                // it's ok
                echo "response content: " . $response->getBody();
        }
} catch ($ex) {
        echo "Exception: $ex";
}


Tyler_H72 05-24-2012 10:25 AM

I'm not sure why, but that code gives me an 'unexpected T_VARIABLE' error. I have checked it six times, and there are no missing semicolons or unbalanced parens/brackets. I can't find any incorrect syntax at all, but I'm still getting that error.

eantoranz 05-24-2012 10:29 AM

You sure have a syntax error. Can you paste your code here or in a pastebin?

Tyler_H72 05-24-2012 10:35 AM

Code:

<html>
<body>
test
<?php


echo "test";

$req = new HTTP_Request2();
$req->setUrl("$whateverurl");
try {
        $response = $req->send();
echo "test1";
        $responseCode = $response->getStatus();
echo "test2";
        if ($responseCode != 200) {
echo "test3";
        } else {
                echo "response content: " . $response->getBody();
        }
} catch($ex) {
        echo "Exception: $ex";
}



?>
</body>
</html>




I'm going to feel really stupid when this turns out to be a semicolon that I overlooked six consecutive times...

eantoranz 05-24-2012 10:41 AM

I guess it was my fault:

Code:

} catch (Exception $e) {

bertlef 05-24-2012 10:41 AM

I fixed the 'unexpected T_VARIABLE' error with

catch (Exception $e)

But then I get another error, the rest of the code would be very useful.

eantoranz 05-24-2012 10:43 AM

Probably that the class is unknown? You have to require the HTTP/Request2 library.

Tyler_H72 05-24-2012 10:45 AM

Yeah, I figured there would be more to do once I got that part working, but I wanted to start with getting it to run. And I probably should have seen that as well- I have been working on getting PHP figured out for a couple days now, but I guess I'm not picking it up very quickly.

eantoranz 05-24-2012 10:48 AM

Take it easy. How do gringos say it? Slowly but surely? That's the motto for development.

Tyler_H72 05-24-2012 10:53 AM

lol- I appreciate the 'gringo'. And thank you very much- you've helped a lot. One problem down, 999,999 to go.

eantoranz 05-24-2012 10:55 AM

You're welcome, dude.

bertlef 05-24-2012 10:56 AM

One step at time.
I don't have that pear library so my help is quite limited at the moment.
I am still not really sure which is your exact problem, it is easy to help if you are very verbose about what is going on and what your exact problem is.

eantoranz 05-24-2012 10:59 AM

You can download the library instead of using pear.

http://download.pear.php.net/package...est2-2.1.1.tgz

Tyler_H72 05-24-2012 11:04 AM

Well Bertlef, this particular problem is solved. The code is sending the request off, and displaying the response properly. My next step is to figure out how to deal with the authentication, which is impossible to work with. Thanks though!

bertlef 05-24-2012 11:08 AM

eantoranz: thanks, I guess I won't be needing it now :)

Awesome then! I'm glad you where able to sort it out.


All times are GMT -5. The time now is 02:57 AM.