平成30/ 2018-10-22 00:48
php Http ステータスコード 取得

fsockopenでステータスコード取得できるみたいだ。
ソースコードは以下。
vi fsockopen.php

<?php
$url = "mobile-search.yahoofs.jp";

$fp = fsockopen($url, 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)\n";
} else {
    // $out = "GET / HTTP/1.1\r\n";
    //echo "$errstr ($errno)\n";
    $out = "GET /simulator?__yahoo_carrier__=sbm&__yahoo_url__=*RncVPIgOKS3pBbQxPo2iqZXlJkG81t.LCUR4W9.mwKZ0lTC12Old6.IlrJ3ZBHaiPyDiRorOO6wRdPh0_NGx5r0t59JkNkG_A2tYYliBmLTN7mBYLeA4pbOXtvs.mUM7SNZ5w3HLJvpcQI_KDLfLM7Mag8qQGMZYZTA_TSVx2d77Q90jdIMpWtwusrkpDFmUxCflAtjWmjEurg2YmOjaC.WOcELUSKsn3pEQ9N9.hUQ91LPh80FaQdJbEpMOeRI.mY1ArCRjUsFW8GRYC8UPk8M1cPzNhiPFLig08PoViRDfJilboBqAw01Wj3WYEe0-&__yahoo_script__=on HTTP/1.1\r\n";
    // $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>



以下の解説すると、1行目がステータスコード。最後の「 0 」は意味不明。
HTTP/1.1 400 Bad Request

$ php fsockopen.php
HTTP/1.1 400 Bad Request
Date: Mon, 06 Jun 2011 07:36:35 GMT
Cache-Control: private
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1

d9b
<!doctype html public "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head><title>Yahoo! - 400 Bad Request</title><style>
/* nn4 hide */
/*/*/
body {font:small/1.2em arial,helvetica,clean,sans-serif;font:x-small;text-align:center;}table {font-size:inherit;font:x-small;}
html>body {font:83%/1.2em arial,helvetica,clean,sans-serif;}input {font-size:100%;vertical-align:middle;}p, form {margin:0;padding:0;}
p {padding-bottom:6px;margin-bottom:10px;}#doc {width:48.5em;margin:0 auto;border:1px solid #fff;text-align:center;}#ygma {text-align:right;margin-bottom:53px}
#ygma img {float:left;}#ygma div {border-bottom:1px solid #ccc;padding-bottom:8px;margin-left:152px;}#bd {clear:both;text-align:left;width:75%;margin:0 auto 20px;}
h1 {font-size:135%;text-align:center;margin:0 0 15px;}legend {display:none;}fieldset {border:0 solid #fff;padding:.8em 0 .8em 4.5em;}
form {position:relative;background:#eee;margin-bottom:15px;border:1px solid #ccc;border-width:1px 0;}
#s1p {width:15em;margin-right:.1em;}
form span {position:absolute;left:70%;top:.8em;}form a {font:78%/1.2em arial;display:block;padding-left:.8em;white-space:nowrap;background: url(http://us.i1.yimg.com/us.yimg.com/i/s/bullet.gif) no-repeat left center;}
form .sep {display:none;}.more {text-align:center;}#ft {padding-top:10px;border-top:1px solid #999;}#ft p {text-align:center;font:78% arial;}
/* end nn4 hide */
</style></head>
<body><div id="doc">
<div id="ygma"><a href="http://us.rd.yahoo.com/400/*http://www.yahoo.com"><img
src=http://us.i1.yimg.com/us.yimg.com/i/yahoo.gif
width=147 height=31 border=0 alt="Yahoo!"></a><div><a
href="http://us.rd.yahoo.com/400/*http://www.yahoo.com">Yahoo!</a>
- <a href="http://us.rd.yahoo.com/400/*http://help.yahoo.com">Help</a></div></div>
<div id="bd"><h1>Sorry, Bad Request.</h1>
Your browser sent a request that this server could not understand.<P>
client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23): /simulator<P>
<p>Please check the URL for proper spelling and capitalization. If
you're having trouble locating a destination on Yahoo!, try visiting the
<strong><a
href="http://us.rd.yahoo.com/400/*http://www.yahoo.com">Yahoo! home
page</a></strong> or look through a list of <strong><a
href="http://us.rd.yahoo.com/400/*http://docs.yahoo.com/docs/family/more/">Yahoo!'s
online services</a></strong>. Also, you may find what you're looking for
if you try searching below.</p>
<form name="s1" action="http://us.rd.yahoo.com/400/*-http://search.yahoo.com/search"><fieldset>
<legend><label for="s1p">Search the Web</label></legend>
<input type="text" size=30 name="p" id="s1p" title="enter search terms here">
<input type="submit" value="Search">
<span><a href="http://us.rd.yahoo.com/400/*http://search.yahoo.com/search/options?p=">advanced search</a> <span class=sep>|</span> <a href="http://us.rd.yahoo.com/400/*http://buzz.yahoo.com">most popular</a></span>
</fieldset></form>
<p class="more">Please try <strong><a
href="http://us.rd.yahoo.com/400/*http://help.yahoo.com">Yahoo!
Help Central</a></strong> if you need more assistance.</p>
</div><div id="ft"><p>Copyright &copy; 2011 Yahoo! Inc.
All rights reserved. <a
href="http://us.rd.yahoo.com/400/*http://privacy.yahoo.com">Privacy
Policy</a> - <a
href="http://us.rd.yahoo.com/400/*http://docs.yahoo.com/info/terms/">Terms
of Service</a></p></div>
</div></body></html>
<!-- ps101.mobile.ogk.yahoo.co.jp uncompressed/chunked Mon Jun  6 16:36:35 JST 2011 -->

0