Skip to content

Instantly share code, notes, and snippets.

@hoai
Forked from Kirill-Gorelov/curl.php
Created March 22, 2018 07:21
Show Gist options
  • Save hoai/e30f300b2cfe87fb4a3bac6bb7be6d7c to your computer and use it in GitHub Desktop.
Save hoai/e30f300b2cfe87fb4a3bac6bb7be6d7c to your computer and use it in GitHub Desktop.
<?php
/**************************** простые гет запросы **********************/
/***********
отправить простой 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);
/**************************** Пост запросы **********************/
/***********************
пост запрос. использую его для передачи параметров
********************/
// массив
$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