Skip to content

Instantly share code, notes, and snippets.

@Kirill-Gorelov
Last active August 25, 2022 08:45
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save Kirill-Gorelov/50e412984b10971ba2a0ae6f2f575ebc to your computer and use it in GitHub Desktop.
Save Kirill-Gorelov/50e412984b10971ba2a0ae6f2f575ebc to your computer and use it in GitHub Desktop.
<?php
/********* универсальное решение ****************/
function sendRequest($url, array $params = [], $post = false){
$curl = curl_init();
$headers = array(
'Content-Type: application/json-patch+json',
'accept: application/json',
);
if ($post == true) {
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
} else {
if (!empty($params)) {
$url = $url . '?' . http_build_query($params);
}
curl_setopt($curl, CURLOPT_URL, $url);
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($curl);
curl_close($curl);
return (array)json_decode($result, true);
}
/**************************** простые гет запросы **********************/
/***********
отправить простой get запрос
************/
function get_curl_go($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
$out = curl_exec($curl);
//echo $out;
curl_close($curl);
return $out;
}
//вызываем
$url = 'https://yandex.ru';
$rez = get_curl_go($url);
//вместо это можно использовать
$rez = file_get_contents($url);
//иногда выходит 403 ошибка то можно так сделать
$ch = curl_init();
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt ($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/html; Accept: application/html; charset=UTF-8']);
$page = curl_exec ($ch);
// $rez = json_decode($result, true);
curl_close($ch);
/**************************** Скачать файл **********************/
/**************
скачать файл file_get_contents
**************/
file_put_contents($new_name, file_get_contents($ame));
/**************
скачать файл curl
**************/
$ch = curl_init('http://img.yandex.net/i/www/logo.png');
$fp = fopen('./images/logo.png', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
/*********
скачать файл через прокси curl
**********/
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0");
curl_setopt($ch, CURLOPT_PROXY, 'прокси:порт');
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
$file = fopen($_SERVER['DOCUMENT_ROOT'].'/img/all/'.$name_img.'.jpg', 'w');
fwrite($file, $result);
fclose($file);
/*********
скачать файл через прокси file_get_contents
**********/
$ctext_param = array(
'http' => array(
'proxy' => 'tcp://прокси:порт',
'request_fulluri' => True,
),
);
$hd_ctext = stream_context_create($ctext_param);
// Now all file stream functions can use this context.
$tmp_imfg = file_get_contents($link, False, $hd_ctext);
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/img/all/'.$result['cur'].'.jpg', $tmp_imfg);
/********
просто get запрос через file_get_contents с параметрами
*********/
$url = 'http://api1.teletopiasms.no/httpbridge2/';
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query(
array(
'auth' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'to' => '47xxxxxxxx',
'from' => '2105',
'type' => 'text',
'data' => utf8_encode('Hello, world! (æøåÆØÅ)'),
'price' => 0
)
),
'timeout' => 60
)
));
$resp = file_get_contents($url, FALSE, $context);
print_r($resp);
/***********
curl get
************/
$url = 'http://api.prostor-sms.ru/messages/v2/send/';
$params = array(
'phone' => $phone,
'password' => $password,
'login' => $login,
'text' => $message,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$result = curl_exec($ch);
if(curl_errno($ch) !== 0) {
return false;
}
curl_close($ch);
// print_r($result);
/**************************** Пост запросы **********************/
/***********************
пост запрос. использую его для передачи параметров
********************/
// массив
$post = array( 'email' => $email);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
$out = curl_exec($curl);
echo $out;
curl_close($curl);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment