Good afternoon, dear community. Actually the task is this - there is some text, I need to add it to the "Original Texts" in Yandex. I try to deal with curl and Yandex api (I read the Yandex documentation for developers, I read the documentation on curl, I took lessons on curl).
Tell me, please, how to write queries using curl under certain conditions, which are specified in the documentation. Often I do not understand what exactly needs to be done and how to make a request. Or read how to do it. I read a lot of the most banal and superficial lessons and stuff, they really do not give any specifics and understanding so that I can go with them and work with Yandex api like this on the move.
At the moment I got token and user_id. But I don’t understand how to make requests further, although everything is indicated in the documentation ( https://tech.yandex.ru/webmaster/doc/dg/reference/host-original-texts-post-docpage/ ). I don’t really understand:
1) First, what standard curl_setopt should the query have?
2) Secondly, what does it mean to pass in the body of the request the main text. How to do it?
3) In general, what exactly can and should be read in order to understand how it all works and how and what to make up if I suddenly work with other api.
Code:
$client_id = "строка"; $client_secret = "строка"; if (!isset($_GET["code"])) { Header("Location: https://oauth.yandex.ru/authorize?response_type=code&client_id=".$client_id); die(); } $result=$this->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') ); //Получение token'а if ($result["code"]==200) { $result["response"]=json_decode($result["response"],true); $token=$result["response"]["access_token"]; } else { echo "Код ошибки: ".$result["code"]; } //Получение user_id $result=$this->getStat( 'https://api.webmaster.yandex.net/v3/user/', array('Authorization: OAuth '.$token) ); $result["response"]=json_decode($result["response"],true); $user_id=$result["response"]["user_id"]; //Отправка текста на яндекс $result=$this->postArticle( "POST https://api.webmaster.yandex.net/v3/user/$user_id/hosts/$client_id/original-texts/", array( 'content' => "asdfsdfwaedfadsads" ), array('Authorization: OAuth '.$token,'Content-type: application/xml') ); dd($result); echo "Код: ".$result["code"]; return view('api'); } //Получение токена public 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); } //Получение user_id public function getStat($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); } //Отправка текста в яндекс "Оригинальные тексты" public function postArticle($url,$peremen,$headers) { $data=json_encode($peremen) $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); }