如何发出HTTP POST请求?

时间:2010-06-21 12:59:29

标签: php curl

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

文档说我需要发布一个发布到墙上。

2 个答案:

答案 0 :(得分:4)

值得一提的是,MANCHUCK建议使用cURL并不是此功能的最佳方式,因为cURL不是核心PHP扩展。管理员必须手动编译/启用它,并且可能并非在所有主机上都可用。正如我在博客上已经指出的那样 - PHP has native support for POSTing data从PHP 4.3版开始(8年前发布!)。

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

答案 1 :(得分: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);
相关问题