I want to communicate one php file with another. Code first:

<?php $url = 'ссылка'; 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; } ?> 

Code of another file:

 <?php var_dump($_POST); ?> 

The first check passes, allegedly sends. And the second does not see at all. Tell me, please, what am I doing wrong?

  • I may not rummage, but he seems to be there. in the line: $ response = $ http-> send (); where, in http I put an array. Or am I not right? - Kira
  • I missed it :) - splash58
  • and in the $response there is something? print_r what will show? maybe $response->getResponseCode() see? - Alexey Shimansky
  • I do not really understand, so just show the output uploads.ru/FNrxW.png for print_r In the second case, about the same output. - Kira
  • one
    If you do not write getBody() , but getResponseBody() , will something appear? instead of echo print_r or var_dump would be var_dump ...... ..... and I have some doubts about the var_dump($_POST); account var_dump($_POST); in the second file ... - Alexey Shimansky

1 answer 1

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)); 
  • He did not give a mistake. Here is a conclusion. uploads.ru/FNrxW.png Only I added print_r there for respons. - Kira
  • Strange, but your output (cURL) doesn't work for me either. - Kira
  • Try код-один-в-один , only your paths. Just see what error flaunts flaunts. - borodatych
  • I am direct to the code. Just put your way. Anyway, the second file shows nothing. - Kira
  • If you are directly trying to run Script2 , then you will not see anything, since the $_POST variable does not exist. The answer needs to be caught in Script1 . - borodatych