Add error output:
<?php error_reporting(E_ALL); ini_set("display_errors", 1); $url = '/path/to/script2.php'; if ($http = new HttpRequest($url, HttpRequest::METH_POST)){ echo "robit"; } else { echo "ne robit"; } $http->setOptions(array('cookies' => array('lang' => 'de'))); $http->addPostFields(array( 'firstData' => 'myData', 'secondData' => 'myDataTwo' )); try{ $response = $http->send(); echo $response->getBody(); } catch (HttpException $ex) { echo $ex; }
If there is no necessary extension, like mine, then at startup you will receive:
Fatal error: Class 'HttpRequest' not found in /path/to/script1.php on line 7
If it is, then you need to install the extension:
$ pecl install -f pecl_http-1.7.6
But I would cast a glance in the direction of cURL!
Script1 :
<?php error_reporting(E_ALL); ini_set("display_errors", 1); $request = function($url,$body=NULL){ $userAgent = ($ua=$_SERVER['HTTP_USER_AGENT']) ?$ua :"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_USERAGENT,$userAgent); curl_setopt($ch, CURLOPT_REFERER, "api://domain.ru"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1 ); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,30); /// 5 curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 0); if( $body ){ $body = (is_array($body)) ? http_build_query($body) : $body; curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS,$body); } $result = curl_exec($ch); return $result; }; $url = '/path/to/script2.php'; $post = array( 'firstData' => 'myData', 'secondData' => 'myDataTwo' ); $response = $request($url,$post); $response = json_decode($response); print"<pre>\n";print_r($response);print"</pre>\n";exit;
Script2 :
<?php die(json_encode($_POST));
$responsethere is something?print_rwhat will show? maybe$response->getResponseCode()see? - Alexey ShimanskygetBody(), butgetResponseBody(), will something appear? instead ofechoprint_rorvar_dumpwould bevar_dump...... ..... and I have some doubts about thevar_dump($_POST);accountvar_dump($_POST);in the second file ... - Alexey Shimansky