Asked  7 Months ago    Answers:  5   Viewed   34 times

I wish to make a simple GET request to another script on a different server. How do I do this?

In one case, I just need to request an external script without the need for any output.

make_request('http://www.externalsite.com/script1.php?variable=45'); //example usage

In the second case, I need to get the text output.

$output = make_request('http://www.externalsite.com/script2.php?variable=45');
echo $output; //string output

To be honest, I do not want to mess around with CURL as this isn't really the job of CURL. I also do not want to make use of http_get as I do not have the PECL extensions.

Would fsockopen work? If so, how do I do this without reading in the contents of the file? Is there no other way?

Thanks all

Update

I should of added, in the first case, I do not want to wait for the script to return anything. As I understand file_get_contents() will wait for the page to load fully etc?

 Answers

61

file_get_contents will do what you want

$output = file_get_contents('http://www.example.com/');
echo $output;

Edit: One way to fire off a GET request and return immediately.

Quoted from http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html

function curl_post_async($url, $params)
{
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }
    $post_string = implode('&', $post_params);

    $parts=parse_url($url);

    $fp = fsockopen($parts['host'],
        isset($parts['port'])?$parts['port']:80,
        $errno, $errstr, 30);

    $out = "POST ".$parts['path']." HTTP/1.1rn";
    $out.= "Host: ".$parts['host']."rn";
    $out.= "Content-Type: application/x-www-form-urlencodedrn";
    $out.= "Content-Length: ".strlen($post_string)."rn";
    $out.= "Connection: Closernrn";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}

What this does is open a socket, fire off a get request, and immediately close the socket and return.

Wednesday, March 31, 2021
 
anjan
answered 7 Months ago
53

As salathe mentioned:

The clue is in the class name, you're creating SimpleXML elements.

It makes sense to me now that a SimpleXMLElement cannot be "empty". It must be a valid XML element, which entails having a tag name and opening and closing tags (e.g. <feature></feature> or <body></body>).

This would seem to imply that SimpleXMLElement was created for parsing, rather than building, XML documents. That being said, I found it very easy to build a document from scratch. The class does a lot of nice things automatically, including keeping everything compact and outputting the XML version number at the top (<?xml version="1.0"?>).

I would recommend this approach to anyone who needs to use PHP to construct small XML documents. It beats echoing out the tags as strings any day.

Thank you, everyone, for your comments!

Wednesday, March 31, 2021
 
LaKaede
answered 7 Months ago
81

sorry for abusing the answer field for what should probably be a comment but I can't really think of a way to present this info properly otherwise. I honestly don't know yet how to whip them comments into any properly displayed and formatted shape. :-|

Anyway, since your code should be working fine but for you it always retuns "everything OK" while you check for a HTTP response, it could be that your ISP is doing DNS-highjacking, which basically is returning fake ip-address data, redirecting you to their own server, usually for monetary gains under the guise of userfriendlyness. (ISP-page that shows "this page doesn't exist" combined with ads or the offer of services such as domain registration, etc..)

an easy way to test is just to ask for DNS data with an application like dig. A query to a properly functioning DNS server for a non-existing domain should return NXDOMAIN as its status:

so when we check for a totally fake domain notexisting.fake with the following command: dig A notexisting.fake., this is what it generally should give:

harald@Midians_Gate:~$ dig A notexisting.fake.

; <<>> DiG 9.7.3 <<>> A notexisting.fake.
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 28725
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0

;; QUESTION SECTION:
;notexisting.fake.              IN      A

;; AUTHORITY SECTION:
.                       1780    IN      SOA     a.root-servers.net. nstld.verisign-grs.com. 2012061700 1800 900 604800 86400

;; Query time: 24 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Jun 17 18:05:10 2012
;; MSG SIZE  rcvd: 109

as you can see,asking for this nonexisting domain notexisting.fake. returns us

;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 28725

Whereas when we query for google.com, with dig A google.com.

harald@Midians_Gate:~$ dig A google.com.

; <<>> DiG 9.7.3 <<>> A google.com.
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13223
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;google.com.                    IN      A

;; ANSWER SECTION:
google.com.             300     IN      A       74.125.132.138
google.com.             300     IN      A       74.125.132.139
google.com.             300     IN      A       74.125.132.101
google.com.             300     IN      A       74.125.132.113
google.com.             300     IN      A       74.125.132.100
google.com.             300     IN      A       74.125.132.102

;; Query time: 29 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Jun 17 18:05:20 2012
;; MSG SIZE  rcvd: 124

our query returns us the proper NOERROR

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13223
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 0

and returns us the ip address found for the A record

;; ANSWER SECTION:
google.com.             300     IN      A       74.125.132.138
google.com.             300     IN      A       74.125.132.139
google.com.             300     IN      A       74.125.132.101
google.com.             300     IN      A       74.125.132.113
google.com.             300     IN      A       74.125.132.100
google.com.             300     IN      A       74.125.132.102

So if your query for the fake domain returns an ip address then you know the problem is with your DNS and your isp no doubt redirects every non-existing request to a server of their own, messing with your 'is my site up strategy' since you'll get a 200 OK status but it'll effectively be from an imposter.

If this is the case then you can only:

  • change your DNS server: use Google Public DNS (8.8.8.8 and 8.8.4.4) or opendns (208.67.222.222 and 208.67.220.220)
  • plead/complain with your isp for them to change this policy or to supply you with your own non-redirecting DNS server
  • run your own private DNS server for your personal lookups if allowed on your network.
Saturday, May 29, 2021
 
Pupil
answered 5 Months ago
91
<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2&postvar3=value3");

// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS, 
//          http_build_query(array('postvar1' => 'value1')));

// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);

curl_close ($ch);

// Further processing ...
if ($server_output == "OK") { ... } else { ... }
?>
Tuesday, June 1, 2021
 
bumperbox
answered 5 Months ago
26

What I do is put a config.php file in my root directory. This file is included by all PHP files in my project. In that config.php file, I then do the following;

define( 'ROOT_DIR', dirname(__FILE__) );

Then in all files, I know what the root of my project is and can do stuff like this

require_once( ROOT_DIR.'/include/functions.php' );

Sorry, no bonus points for getting outside of the public directory ;) This also has the unfortunate side affect that you still need a relative path for finding config.php, but it makes the rest of your includes much easier.

Sunday, July 11, 2021
 
mertak
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :