1

I'm trying to write some simple php code that will make a post request and then retrieve a JSON result from the server. It seemed simple to me, but the below code simply doesn't open a connection.

$port = 2057;
$path = "/validate/";
$request = "value1=somevalue&value2=somevalue&value3=somevalue";

$http_request  = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $server\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: " . strlen($request) . "\r\n";
$http_request .= "\r\n";
$http_request .= $request;

$response = '';

if( false == ( $fs = @fsockopen($server, $port) ) ) {
            die ('Could not open socket');
}

fwrite($fs, $http_request);

while ( !feof($fs) )
{
    $response .= fgets($fs, 1160);
}

fclose($fs);

In addition I've tried a more simple approach with:

$handle = fopen('http://localhost:2057/validate/?'.$request, "r");

or

$response = file_get_contents('http://localhost:2057/validate/' . $request);

but both of these approaches just time out.

I'm trying to connect to a development server I'm running in Visual Studio, so I'm not sure if that has anything to do with the timeout/connection issues.

Open to any suggestions here as long as they are built in PHP.

4
  • do you have telnet installed? If not take a look here. try to connect to your host via telnet, to see if the host is reachable, if not it could be a firewall problem. Commented Jun 10, 2010 at 21:01
  • I can write a similar query in JS and have no trouble reaching the host, so I don't think its a firewall problem. Commented Jun 10, 2010 at 21:08
  • is it safe to assume in the first example you didn't copy the line that says $server = "localhost";? Commented Jun 11, 2010 at 0:33
  • Do you have access to the web server logs? I frequently run PHP on systems that has something akin to SELinux installed. Generally speaking, it won't even let you get a file handle unless its been explicitly told to, much less open a TCP connection. Can you tail the httpd logs while you are doing this? Commented Jun 11, 2010 at 4:20

3 Answers 3

2

There are plenty of pure-PHP HTTP handlers out there that might work better for you.

Try PEAR's HTTP_Client or Zend_Http_Client, both of which you can simply bundle with your application.

If you're dead-set on writing your own, try working with streams. There's a comprehensive set of HTTP stream options to choose from.

Sign up to request clarification or add additional context in comments.

Comments

2

Try using HTTP_Request2; it's not in standard PHP, but you can distribute it with your application so you don't have to worry about whether it's installed or not.

The following is a snippet from a class I use to POST a document to a conversion server; you can post whatever you like and get the results in a similar way.

$request = new HTTP_Request2('http://whereveryouwant:80/foo/');
$request->setMethod(HTTP_Request2::METHOD_POST)
    ->setConfig('timeout', CONVERT_SERVER_TIMEOUT)
    ->setHeader('Content-Type', 'multipart/form-data')
    ->addPostParameter('outputFormat', $outputType);
$request->addUpload('inputDocument', $inputFile);
$result = $request->send();
if ($result->getStatus() == 200) {
    return $result->getBody();
} else {
    return false;
}

Comments

0

It might be simpler to write this by using the http extension : http://fr.php.net/manual/en/function.http-post-data.php

2 Comments

I really bet that it would be however we'll be distributing this and we can't guarantee that people will have extensions so it all has to be in core PHP.
You can distribute Pear modules with your code however; try looking at HTTP_Request2, I use it to submit POST requests and retrieve data easily; I'll post some sample code below if you're interested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.