如何发出HTTP POST请求?

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

文档上说我需要发一个到墙上才能发布。
2个回答

4
值得一提的是,MANCHUCK建议使用cURL并不是实现此功能的最佳方式,因为cURL不是PHP核心扩展。管理员必须手动编译/启用它,并且它可能在所有主机上都不可用。正如我在我的博客中指出的那样 - PHP从4.3版本开始(发布于8年前!)就具有本地支持POST数据的功能。请参考此链接
// 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);

2

使用PHP的curl*函数族。

例如:

<?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);

你还需要为https设置头信息。在设置了以下两个选项后,我成功让代码运行起来了: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); - Terminal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接