Asked  2 Years ago    Answers:  5   Viewed   66 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

3

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.

Monday, August 8, 2022
4

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:

[email protected]_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.

[email protected]_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.
Sunday, August 21, 2022
 
4
<?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 { ... }
?>
Saturday, September 17, 2022
3

You have to $compile your inner html like

.directive('autotranslate', function($interpolate, $compile) {
    return function(scope, element, attr) {
      var html = element.html();
      debugger;
      html = html.replace(/[[(w+)]]/g, function(_, text) {
        return '<span translate="' + text + '"></span>';
      });
      element.html(html);
      $compile(element.contents())(scope); //<---- recompilation 
    }
  })
Tuesday, September 20, 2022
4

You could add a regular do-nothing function to your codebase.

function NOP(varargin)
%NOP Do nothing
%
% NOP( ... )
%
% A do-nothing function for use as a placeholder when working with callbacks
% or function handles.

% Intentionally does nothing

Then you can use a function handle to it instead of to an anonymous function wherever you want to no-op something out.

debug_disp = @NOP;

Now it's somewhat self-documenting, making it explicit that you intended to do nothing, instead of grabbed the wrong input for disp(). It will be apparent in the source code, plus, when you're in the debugger and examining variables holding function handles, it'll show up as "@NOP", which may be more readable than an anonymous handle. And you can get a list of all nopped-out operations in the "profile report" output by looking at a list of callers to NOP.

You could also use Matlab's built-in @deal, which in the degenerate case does nothing and returns nothing.

Tuesday, October 4, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages