S
S
SlavaMorg2016-11-18 15:29:57
PHP
SlavaMorg, 2016-11-18 15:29:57

How to rewrite code from file_get_contents to CURL?

Hello.
I am developing for myself one thing in php, which uses api vk. I didn’t want to go deep into the theory, so I just took a ready-made example and remade it to fit my needs. Here's what happened:

function get_group_info($group_vkid){
  $url = 'https://api.vk.com/method/groups.getById';
  $params = array(
    'group_id' => $group_vkid,
    'fields' => 'members_count',
    'access_token' => '6222b2f6a4afc0e0dd327db7bd0d84cd91726acffee9ae8e338c09997d3a184879e02ae632764156e3edf',
    'v' => '5.59'
  );

  $result = file_get_contents($url, false, stream_context_create(array(
    'http' => array(
      'method'  => 'POST',
      'header'  => 'Content-type: application/x-www-form-urlencoded',
      'content' => http_build_query($params)
    )
  )));
  $result = json_decode($result, 1);
  return $result[response][0];
}

The feature worked fine on the local server. When transferred to the "real" error got out:
file_get_contents( https://api.vk.com/method/groups.getById): failed to open stream: Connection refused
I read on the forums. As I understand it, the servers forbid this function and I need to rewrite everything on CURL. Again, I did not want to delve into the theory and was looking for something ready. I more or less understood how to use CURL, but I didn’t understand how to transfer the context with it. Please explain how I can rewrite this code in CURL, if possible. And if not possible, just advise what to do.
Thank you in advance)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tommy_13, 2016-11-18
@SlavaMorg

rather closed outgoing connections. Curl won't work there.

D
DVamp1r3, 2016-11-18
@DVamp1r3

lornajane.net/posts/2011/posting-json-data-with-ph...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question