2016-02-13 26 views
-5

我喜欢用curl发布一个JSON对象。我所有的就是这样一段代码:如何做一个PHP curl帖子

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中做到这一点?你能举个例子吗?

+1

我投票结束这个问题作为题外话,因为该手册有例子。 http://php.net/manual/en/curl.examples-basic.php – chris85

+0

在php文档中的例子并不是非常有用的说实话 –

回答

0
// 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

SO刚才的例子太多:

PHP + curl, HTTP POST sample code?

0

或者没有卷曲,非常通用的模式,我用它来保持依赖下。

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

?> 

如果Web服务器似乎并不喜欢您的文章,它可能期待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 
     ) 
    );