I am trying to monitor URL's which are in my database. I an using CURL to determine if the url is still alive or not. I gave a simple condition in if clause as if($httpcode>=200 && $httpcode<300)
{
return 1;
} but when i pass http://apps.facebook.com/chkouabeaddeb to CURL it returns 0. but if I put the same URL in browser it redirects to the application. what can i do to make CURL send me correct response ?
curl -I http://apps.facebook.com/chkouabeaddeb
HTTP/1.1 302 Found
Location: /chkouabeaddeb/
302 (a redirect) is greater than 300, so your code and Facebook are both working exactly as programmed.
You're probably getting a 300-class redirection status from curl, which you're assuming means "dead". It doesn't, it means you need to handle it as a redirection.
Related
I am writing a PHP REST API and trying to redirect a POST request to the GET of the new resource, but when I change the location with header() it stays on the post request. In this isolated example, it goes into a redirect loop both on my live deployment and locally on my PHP built-in web server.
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
http_response_code(303);
header("HTTP/1.1 303 See Other");
header("Location: {$_SERVER['REQUEST_URI']}", true, 303);
exit();
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
echo "Hello World!";
}
When I make a request from Postman, I get the following:
Error: Exceeded maxRedirects. Probably stuck in a redirect loop
I expect "Hello World!" to be echoed to the browser, and I only get this expected output when I run the built-in web server directly on my file: php -S localhost:8000 api/test.php
Shouldn't the request method change to GET when I add and replace a location header?
The issue was the Postman client I was using. I needed to switch off Follow original HTTP Method in order for the request to do the default behavior of switching from POST to GET. And indeed, it works in the browser from the Network tab of the Developer tools.
I'm trying to create a web hook notification. The documentation of the service i want to use requires that i specify a URL where POST requests can be performed. This URL will receive the following object, in json format, and must respond with a Status Code between 200-299.
{
"type": "ping"
}
I don't know how to proceed making my server on localhost respond with a 200 status code. http_response_code(200) works well on live server but nothing seem to be happening on localhost.
Is there any way i can make it work with localhost?
I've included the link to the documentation here (i hope it's not against the rule).
I am thinking that you wouldn't have to send them the response. The webhook would know about the response. If it reached your URL successfully, it would be a 200 OK right off the bat. If the API is requesting a response back then I imagine that you would have to call it back somehow. Is this a well-known API? Any documentation?
The response code is in the response header, not in the content.
PHP defaults to a response code of 200, so if you don't mess with it at all, you should be good.
If you want to set a different response code (202 for example), just call:
http_response_code(202);
Or set the full header yourself:
header('HTTP/1.1 202 Accepted');
Proper way to explicitly set 200 (or any other) status code with http_response_code function is just as following (don't echo or json_encode it):
http_response_code(200);
It should force webserver to use 200 status code in it's response. However, webserver could possibly ignore it. To check what response code your webserver sends, use telnet or any REST tool like Postman
I am trying to catch POST response send to me by external API.
The problem is that POST array is completely empty while I can check in firebug that browser recieved it but with codes 302 FOUND and second (with same body) with code 307 TEMPORARY REDIRECT:
Is there any way to grab this data inside my script or is this something wrong with server re-directions?
If you are using the CURL library, there are two options that help with your case:
curl_setopt($curl,CURLOPT_HEADER,1);
This returns the response header including the status code. You can see whether 302 is returned.
Or you can simply follow the redirect
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
Edit: sorry just saw you were doing this on the client side.
If this is an AJAX call, you can get the status code in the raw XHR object.
I am writing a small php script that can distinguish two between different kinds of responses from a third-party website.
For the human visitor, recognizing the difference is fairly easy: Response #1 is a bare-bones 404 error page, whereas response #2 redirects to the main page.
For my script, this turns out to be somewhat more difficult. Both types return a '404' status code, file_get_contents() returns empty for both and the "redirect" doesn't really register as a redirect (like I said, there's a '404' status code, not a '30X'). Get_headers() shows no distinction, either (no "Location:" or anything of that sort).
Any way I can get this done?
There are many ways to do a redirect:
the HTTP response codes for redirect (usually 301 and 302) accompanied by the Location: header that contains the URL
HTTP/1.1 302 Found
Location: http://www.example.org
the HTTP header Refresh:; it contains a number of seconds to wait and the new URL:
Refresh: 0; url=http://www.example.org
the HTML meta element that emulates the Refresh HTTP header:
<meta http-equiv="Refresh" content="0; url=http://www.example.org">
Javascript:
<script>document.location = 'http://www.example.org';</script>
Note that there are countless possibilities to redirect using Javascript. What is common to all of them is the usage of the location property of document or window. location is an object of type Location that can be assigned directly using a string or can be changed using its href property or its methods assign() and replace().
If the requests to your URLs does not return any content, the both return status code 404 and no Location: header then check for the presence of the Refresh: header in the response.
You better use curl to make the requests instead of file_get_contents(). curl provides a better control of the headers sent and received.
I would suggest You to make a cURL request to desired site and see what kind of response it is.
As described in PHP manual,
curl_getinfo ($handle, CURLINFO_HTTP_CODE);
will give You an associative array, in it You will find the http_code key, which will hold Your status code.
If redirect case will give You non-30X status code, You can try to fetch redirect url by this:
curl_getinfo ($handle, CURLINFO_REDIRECT_URL);
I am using cURL to get and post some json array to api.natera.com
When I am curl Get to https://api.natera.com/ it returns 200 OK.
but to https://api.natera.com/2.0/jobs/sequencingRun it gives 404 Not Found.
When I am using OPTIONS to above link instead of GET, it returns 200.
Please help
That endpoint returns the following json response...
{"error":"Not Found"}
I don't know of a webserver that returns this response as a standard 404, so you are probably hitting the service you are trying to hit. Is there an API doc for this endpoint? You might need to include some query string args or something.