$client_id = " Id "; $client_secret = " "; // , // state, , if (!isset($_GET["code"])) { Header("Location: https://oauth.yandex.ru/authorize?response_type=code&client_id=".$client_id); die(); } // "" , // $_Get["code"] . . // , . $result=postKeys("https://oauth.yandex.ru/token", array( 'grant_type'=> 'authorization_code', // 'code'=> $_GET["code"], // 'client_id'=>$client_id, 'client_secret'=>$client_secret ), array('Content-type: application/x-www-form-urlencoded') ); // function postKeys($url,$peremen,$headers) { $post_arr=array(); foreach ($peremen as $key=>$value) { $post_arr[]=$key."=".$value; } $data=implode('&',$post_arr); $handle=curl_init(); curl_setopt($handle, CURLOPT_URL, $url); curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($handle, CURLOPT_POST, true); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($handle, CURLOPT_POSTFIELDS, $data); $response=curl_exec($handle); $code=curl_getinfo($handle, CURLINFO_HTTP_CODE); return array("code"=>$code,"response"=>$response); } // , 200, , if ($result["code"]==200) { $result["response"]=json_decode($result["response"],true); $token=$result["response"]["access_token"]; echo $token; }else{ echo "- ! : ".$result["code"]; } // , , , ,
$token=" "; // , function get_stat($url,$headers) { $handle=curl_init(); curl_setopt($handle, CURLOPT_URL, $url); curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); $response=curl_exec($handle); $code=curl_getinfo($handle, CURLINFO_HTTP_CODE); return array("code"=>$code,"response"=>$response); } // , $result["code"] // - id . // https://webmaster.yandex.ru/api/123456789, 123456789 - id // , id $result=get_stat('https://webmaster.yandex.ru/api/me',array('Authorization: OAuth '.$token)); $user_id=str_replace('https://webmaster.yandex.ru/api/','',$result["response"]); // , . // : href="https://webmaster.yandex.ru/api/123456789/hosts, 123456789 - id // , $result=get_stat('https://webmaster.yandex.ru/api/'.$user_id.'/hosts',array('Authorization: OAuth '.$token)); $xml=new SimpleXMLElement($result["response"]); $hosts_xml=$xml->xpath("host"); $hosts=array(); foreach($hosts_xml as $host) { $hosts[(string)$host->name]= array( "name"=>(string)$host->name, "verification_state"=>(string)$host->verification->attributes()->state, "crawling_state"=>(string)$host->crawling->attributes()->state, "virused"=>(string)$host->virused, "last-access"=>(string)$host->{'last-access'}, "tcy"=>(string)$host->tcy, "url-count"=>(string)$host->{'url-count'}, "index-count"=>(string)$host->{'index-count'}, "href"=>(string)$host->attributes()->href ); } unset($hosts_xml); unset($xml); /* , Array ( [domen] => Array ( [name] => domen - [verification_state] => VERIFIED - [crawling_state] => INDEXED - [virused] => false - [last-access] => 2012-11-06T22:54:10 - [tcy] => 150 - [url-count] => 7458 - [index-count] => 6131 - [href] => https://webmaster.yandex.ru/api/id /hosts/id - ) ) */ // // xml, , , , $site_href="https://webmaster.yandex.ru/api/654321/hosts/123456"; // 654321 - user_id, 123456 - site_id $result=get_stat($site_href."/stats",array('Authorization: OAuth '.$token)); $xml=new SimpleXMLElement($result["response"]); $errors=(string)$xml->{'url-errors'}; // $internal-links=(string)$xml->{'internal-links-count'}; // $links=(string)$xml->{'links-count'}; // unset($xml);
$result=curlGet($site_href."/indexed",array('Authorization: OAuth '.$token)); /* : <host> <index-count>238</index-count> <last-week-index-urls> <url>http://example.com/page1.html</url> <url>http://example.com/page2.html</url> </last-week-index-urls> </host> */ $result=curlGet($site_href."/excluded",array('Authorization: OAuth '.$token)); /* : <host> <url-errors count="12"> <url-errors-with-code code=”404”> <count>12</count> <severity>ERROR</severity> </url-errors-with-code> </url-errors> </host>*/
$result=curlGet($site_href."/links",array('Authorization: OAuth '.$token)); /* : <host> <links-count>1436</links-count> <last-week-links> <url>http://example1.com/page1.html</url> <url>http://example2.com/page2.html</url> </last-week-links> </host>*/
Source: https://habr.com/ru/post/157753/
All Articles