Why my code returns an http 0 response instead of expected 200?
I've a php function that checks if a list of URLs are still alive (checking HTTP status code) , and it works pretty well. When the URL can't be reached, the status code displayed is "0" (ex: http://81.200.15.122/mjpg/video.mjpg). But in some cases, even if the URL is alive, the response is "0".
For example, this URL is alive but my code returns a "0" HTTP status code: http://81.149.56.38:8083/mjpg/video.mjpg
If I use an online HTTP checker (https://www.portcheckers.com/http-header-check), it confirms that the status code should be 200.
I thought that the problem could be related with the fact that it's a mjpg video flow URL, but this other similar URL returns the expected 200 status code: http://204.195.155.5/mjpg/video.mjpg
Here's the code:
function get_response($url) {
$handles = curl_init($url);
curl_setopt($handles, CURLOPT_NOBODY, true);
curl_setopt($handles, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handles, CURLOPT_TIMEOUT, 1);
curl_exec($handles);
$httpresponse = curl_getinfo($handles, CURLINFO_HTTP_CODE);
echo ("http status code: ". $httpresponse);
}
Thank you for any help.
php
add a comment |
I've a php function that checks if a list of URLs are still alive (checking HTTP status code) , and it works pretty well. When the URL can't be reached, the status code displayed is "0" (ex: http://81.200.15.122/mjpg/video.mjpg). But in some cases, even if the URL is alive, the response is "0".
For example, this URL is alive but my code returns a "0" HTTP status code: http://81.149.56.38:8083/mjpg/video.mjpg
If I use an online HTTP checker (https://www.portcheckers.com/http-header-check), it confirms that the status code should be 200.
I thought that the problem could be related with the fact that it's a mjpg video flow URL, but this other similar URL returns the expected 200 status code: http://204.195.155.5/mjpg/video.mjpg
Here's the code:
function get_response($url) {
$handles = curl_init($url);
curl_setopt($handles, CURLOPT_NOBODY, true);
curl_setopt($handles, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handles, CURLOPT_TIMEOUT, 1);
curl_exec($handles);
$httpresponse = curl_getinfo($handles, CURLINFO_HTTP_CODE);
echo ("http status code: ". $httpresponse);
}
Thank you for any help.
php
1
Looks to me this could be a timeout issue. curl has two options:CURLOPT_CONNECTTIMEOUT- The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.CURLOPT_TIMEOUT- The maximum number of seconds to allow cURL functions to execute.
– Patrick Simard
Dec 29 '18 at 13:15
I agree with @PatrickSimard. I have tested your code and a command like$ curl -Is http://81.149.56.38:8083/mjpg/video.mjpg | head -n 1as well and both return 200 or 0 eventually. Depending on your needs, try to increase your timeout, ignore some 0 results or fix your timeout
– thicolares
Dec 29 '18 at 13:43
Since increasing the timeout do not resolve my problem, I also tried to check if the URL returns some content using this:<?php $section = file_get_contents('http://81.149.56.38:8083/mjpg/video.mjpg', FALSE, NULL, 20, 14); var_dump($section); ?>but it also returns that there's no content…
– D.H.
Dec 29 '18 at 14:53
add a comment |
I've a php function that checks if a list of URLs are still alive (checking HTTP status code) , and it works pretty well. When the URL can't be reached, the status code displayed is "0" (ex: http://81.200.15.122/mjpg/video.mjpg). But in some cases, even if the URL is alive, the response is "0".
For example, this URL is alive but my code returns a "0" HTTP status code: http://81.149.56.38:8083/mjpg/video.mjpg
If I use an online HTTP checker (https://www.portcheckers.com/http-header-check), it confirms that the status code should be 200.
I thought that the problem could be related with the fact that it's a mjpg video flow URL, but this other similar URL returns the expected 200 status code: http://204.195.155.5/mjpg/video.mjpg
Here's the code:
function get_response($url) {
$handles = curl_init($url);
curl_setopt($handles, CURLOPT_NOBODY, true);
curl_setopt($handles, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handles, CURLOPT_TIMEOUT, 1);
curl_exec($handles);
$httpresponse = curl_getinfo($handles, CURLINFO_HTTP_CODE);
echo ("http status code: ". $httpresponse);
}
Thank you for any help.
php
I've a php function that checks if a list of URLs are still alive (checking HTTP status code) , and it works pretty well. When the URL can't be reached, the status code displayed is "0" (ex: http://81.200.15.122/mjpg/video.mjpg). But in some cases, even if the URL is alive, the response is "0".
For example, this URL is alive but my code returns a "0" HTTP status code: http://81.149.56.38:8083/mjpg/video.mjpg
If I use an online HTTP checker (https://www.portcheckers.com/http-header-check), it confirms that the status code should be 200.
I thought that the problem could be related with the fact that it's a mjpg video flow URL, but this other similar URL returns the expected 200 status code: http://204.195.155.5/mjpg/video.mjpg
Here's the code:
function get_response($url) {
$handles = curl_init($url);
curl_setopt($handles, CURLOPT_NOBODY, true);
curl_setopt($handles, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handles, CURLOPT_TIMEOUT, 1);
curl_exec($handles);
$httpresponse = curl_getinfo($handles, CURLINFO_HTTP_CODE);
echo ("http status code: ". $httpresponse);
}
Thank you for any help.
php
php
asked Dec 29 '18 at 13:05
D.H.D.H.
11
11
1
Looks to me this could be a timeout issue. curl has two options:CURLOPT_CONNECTTIMEOUT- The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.CURLOPT_TIMEOUT- The maximum number of seconds to allow cURL functions to execute.
– Patrick Simard
Dec 29 '18 at 13:15
I agree with @PatrickSimard. I have tested your code and a command like$ curl -Is http://81.149.56.38:8083/mjpg/video.mjpg | head -n 1as well and both return 200 or 0 eventually. Depending on your needs, try to increase your timeout, ignore some 0 results or fix your timeout
– thicolares
Dec 29 '18 at 13:43
Since increasing the timeout do not resolve my problem, I also tried to check if the URL returns some content using this:<?php $section = file_get_contents('http://81.149.56.38:8083/mjpg/video.mjpg', FALSE, NULL, 20, 14); var_dump($section); ?>but it also returns that there's no content…
– D.H.
Dec 29 '18 at 14:53
add a comment |
1
Looks to me this could be a timeout issue. curl has two options:CURLOPT_CONNECTTIMEOUT- The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.CURLOPT_TIMEOUT- The maximum number of seconds to allow cURL functions to execute.
– Patrick Simard
Dec 29 '18 at 13:15
I agree with @PatrickSimard. I have tested your code and a command like$ curl -Is http://81.149.56.38:8083/mjpg/video.mjpg | head -n 1as well and both return 200 or 0 eventually. Depending on your needs, try to increase your timeout, ignore some 0 results or fix your timeout
– thicolares
Dec 29 '18 at 13:43
Since increasing the timeout do not resolve my problem, I also tried to check if the URL returns some content using this:<?php $section = file_get_contents('http://81.149.56.38:8083/mjpg/video.mjpg', FALSE, NULL, 20, 14); var_dump($section); ?>but it also returns that there's no content…
– D.H.
Dec 29 '18 at 14:53
1
1
Looks to me this could be a timeout issue. curl has two options:
CURLOPT_CONNECTTIMEOUT - The number of seconds to wait while trying to connect. Use 0 to wait indefinitely. CURLOPT_TIMEOUT - The maximum number of seconds to allow cURL functions to execute.– Patrick Simard
Dec 29 '18 at 13:15
Looks to me this could be a timeout issue. curl has two options:
CURLOPT_CONNECTTIMEOUT - The number of seconds to wait while trying to connect. Use 0 to wait indefinitely. CURLOPT_TIMEOUT - The maximum number of seconds to allow cURL functions to execute.– Patrick Simard
Dec 29 '18 at 13:15
I agree with @PatrickSimard. I have tested your code and a command like
$ curl -Is http://81.149.56.38:8083/mjpg/video.mjpg | head -n 1 as well and both return 200 or 0 eventually. Depending on your needs, try to increase your timeout, ignore some 0 results or fix your timeout– thicolares
Dec 29 '18 at 13:43
I agree with @PatrickSimard. I have tested your code and a command like
$ curl -Is http://81.149.56.38:8083/mjpg/video.mjpg | head -n 1 as well and both return 200 or 0 eventually. Depending on your needs, try to increase your timeout, ignore some 0 results or fix your timeout– thicolares
Dec 29 '18 at 13:43
Since increasing the timeout do not resolve my problem, I also tried to check if the URL returns some content using this:
<?php $section = file_get_contents('http://81.149.56.38:8083/mjpg/video.mjpg', FALSE, NULL, 20, 14); var_dump($section); ?> but it also returns that there's no content…– D.H.
Dec 29 '18 at 14:53
Since increasing the timeout do not resolve my problem, I also tried to check if the URL returns some content using this:
<?php $section = file_get_contents('http://81.149.56.38:8083/mjpg/video.mjpg', FALSE, NULL, 20, 14); var_dump($section); ?> but it also returns that there's no content…– D.H.
Dec 29 '18 at 14:53
add a comment |
2 Answers
2
active
oldest
votes
cURL returns 0 on timeout. Some URL's can have a higher latancy then others. If cURL reaches the time out it will return 0 because the query is incomplet.
function get_response($url) {
$handles = curl_init($url);
curl_setopt($handles, CURLOPT_NOBODY, true);
curl_setopt($handles, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handles, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($handles, CURLOPT_TIMEOUT, 30);
curl_exec($handles);
$httpresponse = curl_getinfo($handles, CURLINFO_HTTP_CODE);
echo ("http status code: ". $httpresponse);
}
Change the values to optimize your code. You should run this in a cron and save results in your sql table to avoid slowing down the page. You can also add a "last scanned 1h ago" or something like that. Users will have the list served instantly and server side will have planty of time to query every urls.
Thank you for your advices. But even with very large timeout values, I still get a 0. I've looked at othercurloptions that could help, but didn't find anything.
– D.H.
Dec 29 '18 at 14:30
add a comment |
I finally found what was the problem: my hosting server firewall has some ports closed, that do not allow a response for some URLs.
In which case, curl_getinfo() will return false, not 0
– symcbean
Jan 5 at 18:04
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53969792%2fwhy-my-code-returns-an-http-0-response-instead-of-expected-200%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
cURL returns 0 on timeout. Some URL's can have a higher latancy then others. If cURL reaches the time out it will return 0 because the query is incomplet.
function get_response($url) {
$handles = curl_init($url);
curl_setopt($handles, CURLOPT_NOBODY, true);
curl_setopt($handles, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handles, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($handles, CURLOPT_TIMEOUT, 30);
curl_exec($handles);
$httpresponse = curl_getinfo($handles, CURLINFO_HTTP_CODE);
echo ("http status code: ". $httpresponse);
}
Change the values to optimize your code. You should run this in a cron and save results in your sql table to avoid slowing down the page. You can also add a "last scanned 1h ago" or something like that. Users will have the list served instantly and server side will have planty of time to query every urls.
Thank you for your advices. But even with very large timeout values, I still get a 0. I've looked at othercurloptions that could help, but didn't find anything.
– D.H.
Dec 29 '18 at 14:30
add a comment |
cURL returns 0 on timeout. Some URL's can have a higher latancy then others. If cURL reaches the time out it will return 0 because the query is incomplet.
function get_response($url) {
$handles = curl_init($url);
curl_setopt($handles, CURLOPT_NOBODY, true);
curl_setopt($handles, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handles, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($handles, CURLOPT_TIMEOUT, 30);
curl_exec($handles);
$httpresponse = curl_getinfo($handles, CURLINFO_HTTP_CODE);
echo ("http status code: ". $httpresponse);
}
Change the values to optimize your code. You should run this in a cron and save results in your sql table to avoid slowing down the page. You can also add a "last scanned 1h ago" or something like that. Users will have the list served instantly and server side will have planty of time to query every urls.
Thank you for your advices. But even with very large timeout values, I still get a 0. I've looked at othercurloptions that could help, but didn't find anything.
– D.H.
Dec 29 '18 at 14:30
add a comment |
cURL returns 0 on timeout. Some URL's can have a higher latancy then others. If cURL reaches the time out it will return 0 because the query is incomplet.
function get_response($url) {
$handles = curl_init($url);
curl_setopt($handles, CURLOPT_NOBODY, true);
curl_setopt($handles, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handles, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($handles, CURLOPT_TIMEOUT, 30);
curl_exec($handles);
$httpresponse = curl_getinfo($handles, CURLINFO_HTTP_CODE);
echo ("http status code: ". $httpresponse);
}
Change the values to optimize your code. You should run this in a cron and save results in your sql table to avoid slowing down the page. You can also add a "last scanned 1h ago" or something like that. Users will have the list served instantly and server side will have planty of time to query every urls.
cURL returns 0 on timeout. Some URL's can have a higher latancy then others. If cURL reaches the time out it will return 0 because the query is incomplet.
function get_response($url) {
$handles = curl_init($url);
curl_setopt($handles, CURLOPT_NOBODY, true);
curl_setopt($handles, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handles, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($handles, CURLOPT_TIMEOUT, 30);
curl_exec($handles);
$httpresponse = curl_getinfo($handles, CURLINFO_HTTP_CODE);
echo ("http status code: ". $httpresponse);
}
Change the values to optimize your code. You should run this in a cron and save results in your sql table to avoid slowing down the page. You can also add a "last scanned 1h ago" or something like that. Users will have the list served instantly and server side will have planty of time to query every urls.
answered Dec 29 '18 at 13:49
Patrick SimardPatrick Simard
1,05911024
1,05911024
Thank you for your advices. But even with very large timeout values, I still get a 0. I've looked at othercurloptions that could help, but didn't find anything.
– D.H.
Dec 29 '18 at 14:30
add a comment |
Thank you for your advices. But even with very large timeout values, I still get a 0. I've looked at othercurloptions that could help, but didn't find anything.
– D.H.
Dec 29 '18 at 14:30
Thank you for your advices. But even with very large timeout values, I still get a 0. I've looked at other
curl options that could help, but didn't find anything.– D.H.
Dec 29 '18 at 14:30
Thank you for your advices. But even with very large timeout values, I still get a 0. I've looked at other
curl options that could help, but didn't find anything.– D.H.
Dec 29 '18 at 14:30
add a comment |
I finally found what was the problem: my hosting server firewall has some ports closed, that do not allow a response for some URLs.
In which case, curl_getinfo() will return false, not 0
– symcbean
Jan 5 at 18:04
add a comment |
I finally found what was the problem: my hosting server firewall has some ports closed, that do not allow a response for some URLs.
In which case, curl_getinfo() will return false, not 0
– symcbean
Jan 5 at 18:04
add a comment |
I finally found what was the problem: my hosting server firewall has some ports closed, that do not allow a response for some URLs.
I finally found what was the problem: my hosting server firewall has some ports closed, that do not allow a response for some URLs.
answered Jan 5 at 18:00
D.H.D.H.
11
11
In which case, curl_getinfo() will return false, not 0
– symcbean
Jan 5 at 18:04
add a comment |
In which case, curl_getinfo() will return false, not 0
– symcbean
Jan 5 at 18:04
In which case, curl_getinfo() will return false, not 0
– symcbean
Jan 5 at 18:04
In which case, curl_getinfo() will return false, not 0
– symcbean
Jan 5 at 18:04
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53969792%2fwhy-my-code-returns-an-http-0-response-instead-of-expected-200%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Looks to me this could be a timeout issue. curl has two options:
CURLOPT_CONNECTTIMEOUT- The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.CURLOPT_TIMEOUT- The maximum number of seconds to allow cURL functions to execute.– Patrick Simard
Dec 29 '18 at 13:15
I agree with @PatrickSimard. I have tested your code and a command like
$ curl -Is http://81.149.56.38:8083/mjpg/video.mjpg | head -n 1as well and both return 200 or 0 eventually. Depending on your needs, try to increase your timeout, ignore some 0 results or fix your timeout– thicolares
Dec 29 '18 at 13:43
Since increasing the timeout do not resolve my problem, I also tried to check if the URL returns some content using this:
<?php $section = file_get_contents('http://81.149.56.38:8083/mjpg/video.mjpg', FALSE, NULL, 20, 14); var_dump($section); ?>but it also returns that there's no content…– D.H.
Dec 29 '18 at 14:53