markdown cURL使用JSON.md获取和POST

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown cURL使用JSON.md获取和POST相关的知识,希望对你有一定的参考价值。

cURL GET and POST with JSON
===========================

>   [p6abhu][o] January 28, 2016


Here are two PHP scripts I just wrote that use `curl` and `curl_setopt`. The first example makes a __GET request__, and the second example makes a __POST request__, and passes __JSON data__ to the web service it accesses.


A PHP curl GET request
----------------------

This first one makes an __HTTP GET request__ and prints the data that is returned by the __URL__ that it hits:

```php
<?php

# An HTTP GET request example

$url = 'http://localhost:8080/stocks';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
echo $data;

?>
```


### A PHP curl POST request, with JSON data

This next one makes an __HTTP POST request__. The way it works is that it sits in the middle of (a) a __Sencha ExtJS__ client and (b) another web service at the __URL shown__. The __Sencha client__ gives this script the data in a simple __POST format__, and this script converts that data to __JSON__, passes it to the __URL shown__, and returns whatever information that service returns directly to the __Sencha client__:

```php
<?php
# An HTTP POST request example
# a pass-thru script to call my Play server-side code.
# currently needed in my dev environment because Apache and Play run on
# different ports. (i need to do something like a reverse-proxy from
# Apache to Play.)
# the POST data we receive from Sencha (which is not JSON)
$id = $_POST['id'];
$symbol = $_POST['symbol'];
$companyName = $_POST['companyName'];

# data needs to be POSTed to the Play url as JSON.
# (some code from http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl)
$data = array("id" => "$id", "symbol" => "$symbol", "companyName" => "$companyName");
$data_string = json_encode($data);

$ch = curl_init('http://localhost:8080/stocks/add');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

echo $result;
?>
```


In summary, if you needed some __PHP__ `curl` examples, including how to set `curl` options with `curl_setopt`, I hope these examples have been helpful.

* * *


```php
<?php
// http://lornajane.net/posts/2011/posting-json-data-with-php-curl
$data = array("name" => "Hagrid", "age" => "36");                                                                    
$data_string = json_encode($data);                                                                                   
                                                                                                                     
$ch = curl_init('http://api.local/rest/users');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   
                                                                                                                     
$result = curl_exec($ch); 
?>
```


[o]: http://j.mp/2jbXgPX "How to use PHP curl GET and POST with JSON web services"


* * *

<http://j.mp/2zhbgAP>

以上是关于markdown cURL使用JSON.md获取和POST的主要内容,如果未能解决你的问题,请参考以下文章

markdown 使用curl从cli获取github API令牌

markdown 使用GitHub API的curl教程

shell中curl请求变更使用markdown

shell中curl请求变更使用markdown

markdown [CURL] Como fazer CURL

markdown 尝试在OSGE4W shell中使用cURL时出现证书错误的解决方法