繁体   English   中英

如何使用CURL从终端发布的PHP中检索JSON数据

[英]How to retrieve JSON data in PHP posted from terminal using CURL

我正在使用安装在ubuntu中的CURL将JSON发送到PHP页面。在那个页面中,我正在尝试解码JSON数据并存储到数据库中。

这是用于发送JSON的命令

 curl -i -X POST -d '{"Customer":{"first_name":"First name","last_name":"last name","email":"email@gmail.com","addresses":{"address1":"some address","city":"city","country":"CA","first_name":"Mother","last_name":"Lastnameson","phone":"555-1212","province":"ON","zip":"123 ABC"}}}' https://phpserver-chaturasan.c9users.io/listener.php

终端输出:

HTTP/1.1 200 OK
date: Tue, 19 Jul 2016 05:32:25 GMT
server: Apache/2.4.7 (Ubuntu)
x-powered-by: PHP/5.5.9-1ubuntu4.17
set-cookie: XDEBUG_SESSION=cloud9ide; expires=Tue, 19-Jul-2016     06:32:25 GMT; Max-Age=3600; path=/
set-cookie: XDEBUG_SESSION=cloud9ide; expires=Tue, 19-Jul-2016 06:32:25 GMT; Max-Age=3600; path=/
vary: Accept-Encoding
content-length: 947
keep-alive: timeout=5, max=100
content-type: text/html
X-BACKEND: apps-proxy

/home/ubuntu/workspace/listener.php:11:
string(258) "{"Customer":{"first_name":"First name","last_name":"last name","email":"email@gmail.com","addresses":{"address1":"some address","city":"city","country":"CA","first_name":"Mother","last_name":"Lastnameson","phone":"555-1212","province":"ON","zip":"123 ABC"}}}"
hello world/home/ubuntu/workspace/listener.php:14:
array(1) {
  'Customer' =>
  array(4) {
    'first_name' =>
    string(10) "First name"
    'last_name' =>
    string(9) "last name"
    'email' =>
    string(15) "email@gmail.com"
    'addresses' =>
    array(8) {
      'address1' =>
      string(12) "some address"
      'city' =>
      string(4) "city"
      'country' =>
      string(2) "CA"
      'first_name' =>
      string(6) "Mother"
      'last_name' =>
      string(11) "Lastnameson"
      'phone' =>
      string(8) "555-1212"
      'province' =>
      string(2) "ON"
      'zip' =>
      string(7) "123 ABC"
    }
  }
}

PHP页面代码:

<?php 
ini_set("allow_url_fopen", true);
/*if(isset($_POST['Customer'])){
    $db = new mysqli("localhost","root","","gen");
    $name = $db->real_escape_string($_POST['Scenes']);
    $query = "INSERT INTO g SET name = '$name'";
    $db->query($query);
}
*/

$jsonStr = file_get_contents("php://input"); //read the HTTP body.
var_dump($jsonStr);
echo "hello world";
$json = json_decode($jsonStr, true);
var_dump($json);
 /*db = new mysqli("localhost","root","","gen");
$query = "INSERT INTO g SET name = '$json'";
$db->query($query);*/
?>

但是页面的输出是

/home/ubuntu/workspace/listener.php:11: string(0) "" hello world/home/ubuntu/workspace/listener.php:14: NULL

现在我如何存储JSON数据?

试试我的来源。 它对我有用!!

$data_string = '{"Customer":{"first_name":"First name","last_name":"last name","email":"email@gmail.com","addresses":{"address1":"some address","city":"city","country":"CA","first_name":"Mother","last_name":"Lastnameson","phone":"555-1212","province":"ON","zip":"123 ABC"}}}';

$ch = curl_init('https://phpserver-chaturasan.c9users.io/listener.php');
$options = array(
    CURLOPT_RETURNTRANSFER => true,     // return web page
    CURLOPT_HEADER         => false,    // don't return headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    CURLOPT_ENCODING       => "",       // handle all encodings
    CURLOPT_USERAGENT      => "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0", // who am i
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
    CURLOPT_TIMEOUT        => 120,      // timeout on response
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    CURLOPT_SSL_VERIFYPEER => false     // Disabled SSL Cert checks
);
curl_setopt_array( $ch, $options );                      
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);
echo $result;

我的结果

在此输入图像描述

我不知道你想要达到的目的。 但是从你的问题来看,我猜这一点

  1. 您正在使用JSON编码数据从CLI(命令行/终端)进行cURL调用。 然后你正在处理数据。
  2. 当您通过Web服务器访问文件时,您希望以某种方式提供数据。

如果是这种情况那么,您需要将数据persistant storagepersistant storage如最基本意义上的平面文件或MySQL的数据库(根据注释掉的代码),然后在需要时检索相同内容,特别是因为php是无国籍,每一个电话都是新的呼唤。

一旦我更清楚地了解您的问题究竟是什么,请进一步更新答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM