【问题标题】:How to do a PHP curl post [closed]如何做一个 PHP curl 帖子 [关闭]
【发布时间】:2016-05-24 22:56:05
【问题描述】:

我喜欢用 curl 发布一个 JSON 对象。我只有那段代码:

curl -X POST \
-H "Accept: application/json" \
-H "X-Access-Token: ###secureToken###" \
-H "Cache-Control: no-cache" \
-d '{
    "frames": [
        {
            "index": 0,
            "text": "SUCCESS",
            "icon": null
        }
    ]
}' \
https://developer.lametric.com/api/V1/dev/widget/update/com.lametric.###appid###

现在究竟要做什么才能在 PHP 中实现这一点?可以发个例子吗?

【问题讨论】:

标签: php curl


【解决方案1】:
// init curl

$handle = curl_init();

// set options/parameters

curl_setopt( $handle, CURLOPT_URL,              'https://developer.lametric.c...');
curl_setopt( $handle, CURLOPT_CUSTOMREQUEST,    "POST");
curl_setopt( $handle, CURLOPT_POSTFIELDS,       'the-json-encoded-data-here' );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER,   true ); // you want to get the response

// set headers 

curl_setopt( $handle, CURLOPT_HTTPHEADER,       array(  'Accept: application/json',
                                                        '....' ) );
// execute the request and get the response

$response = curl_exec( $handle );

// get the status too

$status = curl_getinfo( $handle, CURLINFO_HTTP_CODE );

// release resources

curl_close( $handle );

只是一个例子/介绍。

你初始化 php 的 curl。

设置所有参数。

发送请求。

我不会为你写所有的代码。

PHP 参考很清楚(也有例子)

http://php.net/manual/en/book.curl.php

所以也有例子:

PHP + curl, HTTP POST sample code?

【讨论】:

    【解决方案2】:

    或者没有 curl,我用来降低依赖关系的非常通用的模式。

    <?php 
    
    $reqBody = array(
        'frames' => array(
            'index' => 0,
            'text' => "SUCCESS",
            'icon' => null
        )
    );
    
    
    $bodyString = json_encode($reqBody);
    
    $access_token = "###secureToken###";
    $context_options = array (
        'http' => array (
            'method' => 'POST',
            'header' => "Accept: application/json\r\nX-Access-Token: " . $access_token . "\r\nCache-Control: no-cache\r\nContent-Length: " . strlen($bodyString) . "\r\n",
            'content' => $bodyString
            )
        );
    $context_for_post = stream_context_create($context_options);
    
    $response = file_get_contents($"https://developer.lametric.com/api/V1/dev/widget/update/com.lametric.###appid###", FALSE, $context_for_post);
    
    // Check for errors
    if(!$response){
        die("<h2>ERROR</h2>");
    }
    
    // Decode the response
    $responseData = json_decode($response, TRUE);
    
    // some examples of parsing response json ...
    if ($responseData['message'] != null) {
    
    }
    $this->sessionToken = $responseData['message']['data']['results']['token'];
    if($this->sessionToken === FALSE) {
        die('Failed to Parse Response');
    }
    
    ?>
    

    如果网络服务器似乎不喜欢你的帖子,它可能需要 POST 的表单数据类型,因此请像这样设置正文和标题:

    $bodyString = "------WebKitFormBoundaryiAsuvpNuslAE3Kqx\r\nContent-Disposition: form-data; name=\"json\"\r\n\r\n" . 
        json_encode($reqBody) . 
        "\r\n------WebKitFormBoundaryiAsuvpNuslAE3Kqx--\r\n";
    
    $access_token = "###secureToken###";
    $context_options = array (
        'http' => array (
            'method' => 'POST',
            'header' => "X-Access-Token: " . $access_token . "\r\nCache-Control: no-cache\r\nAccept: application/json\r\nContent-Type: multipart/form-data; boundary=----WebKitFormBoundaryiAsuvpNuslAE3Kqx\r\n" . "Content-Length: " . strlen($bodyString) . "\r\n",
            'content' => $bodyString
            )
        );
    

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多