tags:

views:

989

answers:

3

Here is a snippet of my code

        $fp = fsockopen($s['url'], 80, $errno, $errstr, 5);
        if($fp){
                fwrite($fp, $out);
                fclose($fp);

When I run it, it outputs:

unable to connect to www.mydomain.net/1/file.php:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known

I can't figure out why. Any help would be greatly appreciated.

EDIT: I forgot to mention that I'm using this to submit GET data to the $s['url']

A: 

you are trying to open a socket to a file on the remote host which is not correct. you could make a socket connection (TCP/UDP) to a port number on a remote host. so your code should be like this:

fsockopen('www.mysite.com', 80);

if you are trying to create a file pointer resource to a remote file, you may use the fopen() function. but to do this, you need to specify the application protocol as well.

PHP provides default stream wrappers for URL file opens. based on the schema of the URL the appropriate stream wrapper will be called internally. the URL you are trying to open does not have a valid schema for this solution. make sure there is a schema like "http://" or "ftp://" in it.

so the code would be like this:

$fp = fopen('http://www.mysite.com/path/file.txt');

Besides I don't think the HTTP stream wrapper (that handles actions on file resources on URLs with http schema) supports writing of data. you can use fread() to read contents of a the URL through HTTP, but I'm not sure about writing.

EDIT: from comments and other answers I figured out you would want to send a HTTP request to the specified URL. the methods described in this answer are for when you want to receive data from the remote URL. if you want to send data, you can use http_request() to do this.

farzad
Well what I'm doing is submitting GET data to the $s['url']
Rob
it is ok to replace the hardcoded addresses in the URLs with the data you receive from GET. but make sure there would be a shchema and if not, add a default 'http://' there. use parse_url() function to make sure there is a schema in the URL.
farzad
+2  A: 

You cannot open a connection directly to a path on a remote host using fsockopen. The url www.mydomain.net/1/file.php contains a path, when the only valid value for that first parameter is the host, www.mydomain.net.

If you are trying to access a remote URL, then file_get_contents() is your best bet. You can provide a full URL to that function, and it will fetch the content at that location using a normal HTTP request.

If you only want to send an HTTP request and ignore the response, you could use fsockopen() and manually send the HTTP request headers, ignoring any response. It might be easier with cURL though, or just plain old fopen(), which will open the connection but not necessarily read any response. If you wanted to do it with fsockopen(), it might look something like this:

$fp = fsockopen("www.mydomain.net", 80, $errno, $errstr, 30);
fputs($fp, "GET /1/file.php HTTP/1.1\n");
fputs($fp, "Host: www.mydomain.net\n");
fputs($fp, "Connection: close\n\n"); 

That leaves any error handling up to you of course, but it would mean that you wouldn't waste time reading the response.

zombat
Well I don't need to retrieve the contents, only submit the GET data. Would that particularly matter, or would it be fine anyway?i.e. file_get_contents("http://www.domain.com/file.php?action=this
Rob
it's fine, you can just ignore the return value of file_get_contents.
elias
Thought so. I'll give it a shot and come back
Rob
@Rob - elias is right, you could just ignore the return value. I updated my answer with a crude `fsockopen()` method of manually sending HTTP request headers if you were worried about the overhead of making a full HTTP request (which probably isn't much, unless that URL is returning a ton of info).
zombat
+1  A: 

If you only want to submit GET data to the URL, you should use something straightforward like file_get_contents();

$myGetData = "?var1=val1&var2=val2";
file_get_contents($url.$myGetData);
elias
well hell that makes it all too easy. one sec I'll give it a shot
Rob
This worked perfectly. Thanks
Rob
Welcome to the simple world of PHP xD
elias