z0ddak
12/10/2016 - 1:14 PM

Передача методом cURL

Передача методом cURL

<?php

/***
**** cURL-handler
***/
function file_get_contents_curl($url,$query_str,$ispost=false) {

	$referer = $_SERVER['HTTP_REFERER']; // Who requests a connection
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_REFERER, $referer); // We give where we ask for
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Set the option to curl returned data

	if($ispost===true) {
		curl_setopt($ch, CURLOPT_POST, true);
		if(is_array($query_str)) {
			foreach($query_str as $key=>$value)
			$post_fields .= $key.'='.$value.'&';
	    }
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
	}
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

if (!defined('NEED_URL')) // We set the URL file-handler
  define('NEED_URL', 'http://vash-site.com/.../public/form_handler_cb.php');
  
if($_REQUEST['countadd'] == 1) // If the data were sent from the form
{
  $vote_addcount = $_REQUEST['addcount'];
  $vote_userid = $id;

  // We form an array with the parameters of the form
  $query_str = Array('vote_addcount'=>$vote_addcount,'vote_userid'=>$vote_userid,'vote_countadd'=>'1');

  $ispost=true; // Using POST
  $url = NEED_URL; // This is the address where the request is directed
  $answer = file_get_contents_curl($url,$query_str,$ispost);
  
  // echo 'answer='.$answer; exit; // uncomment to test response
  
  if($answer == 'DONE')
  {
    // 1st action
  }
  else
  {
    // 2nd action
  }
}

?>