How to issue an HTTP POST request?

curl -F 'access_token=...' \
     -F 'message=Hello, Arjun. I like this new API.' \
     https://graph.facebook.com/arjun/feed

The documentation says that I need to publish it for publication on the wall.

+3
source share
2 answers

In php, use the curl * family of functions.

example:

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/arjun/feed');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('access_token' => 'my token',
                                           'message' => 'Hello, Arjun. I like this new API.'));

curl_exec($ch);
+2
source

It is worth mentioning that the MANCHUCK suggestion to use cURL is not the best way for this function, since cURL is not the main extension of PHP. Administrators must compile / enable it manually, and it may not be available on all hosts. And as already stated in my blog - PHP has built-in support for POSTing data , starting with version PHP 4.3 (released 8 years ago!).

// Your POST data
$data = http_build_query(array(
    'param1' => 'data1',
    'param2' => 'data2'
));

// Create HTTP stream context
$context = stream_context_create(array(
    'http' => array(
        'method' => 'POST',
        'header' => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $data
    )
));

// Make POST request
$response = file_get_contents('http://example.com', false, $context);
+3
source

Source: https://habr.com/ru/post/1751008/


All Articles