How to send a POST request of the following form:

POST /gate/ki-site-api/ HTTP/1.1 Host: api.site.ru Cache-Control: no-cache Content-Type: application/x-www-form-urlencoded id=get_pdf&username=username&params=... 

I try to do this:

 <? ini_set('display_errors',1); error_reporting(E_ALL); $url = 'https://api.site.ru/gate/sie-api/'; $data = array('id' => 'make_pdf', 'username' => 'username','account_id' =>'1152289', 'period_id' => '201705'); $params = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data) ) ); $context = stream_context_create($params); $result = file_get_contents($url, true, $context); if ($result === FALSE) var_dump($result); ?> 

I get the answer:

HTML> string (51) "{" err_msg ":" Authorization failed. "," Err_code ": 1}"

From the documentation:

In order to access a specific API method, you need to send a request using the POST method to the ROOT URL. This request must contain the following required parameters: id - the identifier of the method that you intend to call. For example, get_main_id. See the section of the relevant method group. username - your username - username.

I can not understand how to get authorized and get the necessary data. $ url and username in the description are changed. Can I not pass authorization due to the fact that I am performing a request from a third-party site?

  • file_get_contents will not work. Use curl - Vfvtnjd
  • @Vfvtnjd and for what reason will not work? - teran
  • @Vfvtnjd How did you get this? In this case, it is suitable if, of course, the context options are set correctly - hindmost

2 answers 2

Since the API is located on HTTPS, in addition to HTTP, you still need to configure SSL options by adding an appropriate subarray with the ssl key to the options array. If you have a certificate file on the server, then you need to specify the path to it cafile or capath . If you do not have it, then you need to disable the verify_peer and verify_peer_name .

In addition, you are submitting an invalid API URL. In the description of the request, you have the URL 'https://gate/ki-site-api/' , and in fact the file 'https://api.site.ru/gate/sie-api/' is passed to file_get_contents .

As a result, your code should look something like this:

 $url = 'https://api.site.ru/gate/ki-site-api/'; $data = array('id' => 'make_pdf', 'username' => 'username','account_id' =>'1152289', 'period_id' => '201705'); $params = array( 'http' => array( 'header' => 'Content-type: application/x-www-form-urlencoded', 'method' => 'POST', 'content' => http_build_query($data) ), 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, ), ); $context = stream_context_create($params); $result = file_get_contents($url, true, $context); 

    The problem was that the API developer did not indicate that requests could be sent from domains that were given access.

    The script is working. Thank you all for your help!