【问题标题】:How to issue HTTP POST request?如何发出 HTTP POST 请求?
【发布时间】:2010-06-21 12:59:28
【问题描述】:
curl -F 'access_token=...' \
     -F 'message=Hello, Arjun. I like this new API.' \
     https://graph.facebook.com/arjun/feed

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

【问题讨论】:

    标签: php curl


    【解决方案1】:

    值得一提的是,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);
    

    【讨论】:

    • 确实像一个魅力。谢谢。
    【解决方案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);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    相关资源
    最近更新 更多