Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用cUrl从PHP发布JSON对象_Php_Json_Post_Curl - Fatal编程技术网

使用cUrl从PHP发布JSON对象

使用cUrl从PHP发布JSON对象,php,json,post,curl,Php,Json,Post,Curl,我使用php功能创建了一个JSON对象JSON\u encode 目标的表述方式如下: $object_array[]=array('value1' => $value1, 'value2' => $value2); $json_output = json_encode($object_array, JSON_UNESCAPED_SLASHES); 我需要使用PHP的“cUrl”功能将$json_输出发布到URL 有人能根据以上代码提出建议吗?urlencode()是以编码形式发

我使用php功能创建了一个JSON对象
JSON\u encode

目标的表述方式如下:

$object_array[]=array('value1' => $value1, 'value2' => $value2);

$json_output = json_encode($object_array, JSON_UNESCAPED_SLASHES);
我需要使用PHP的“cUrl”功能将
$json_输出发布到URL

有人能根据以上代码提出建议吗?

urlencode()
是以编码形式发布URL的最佳方法

$json_output = json_encode($object_array, JSON_UNESCAPED_SLASHES);
$url = 'www.example.com?url='.urlencode($json_output);
您将在url参数的
urldecode()
之后获得数组

$json = urldecode($_GET['url']);
urlencode()
是以编码形式发布URL的最佳方法

$json_output = json_encode($object_array, JSON_UNESCAPED_SLASHES);
$url = 'www.example.com?url='.urlencode($json_output);
您将在url参数的
urldecode()
之后获得数组

$json = urldecode($_GET['url']);

使用curl发布json对象

$data = array('value1' => $value1, 'value2' => $value2);                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://api.local/rest/users');   // where to post                                                                   
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);

使用curl发布json对象

$data = array('value1' => $value1, 'value2' => $value2);                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://api.local/rest/users');   // where to post                                                                   
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);


您可以使用url_编码请参考:我需要将该对象发布到另一个不会使用PHP解码的服务器。是否可以使用某种东西发送准确的对象?类似于
$.post(“url”,$json_输出)
?如何集成php和jquery/ajax?可能很有趣吧。似乎PHP‘rawurlencode’可能有用?@RyanVincent但我的问题是,我需要以某种方式将JSON对象传递到另一台服务器上,而不是从我创建它的地方,因此我需要以某种方式定义一个url。您可以使用url_encode。请参阅:我需要将该对象发布到另一台不会使用PHP进行解码的服务器上。是否可以使用某种东西发送准确的对象?类似于
$.post(“url”,$json_输出)
?如何集成php和jquery/ajax?可能很有趣吧。似乎PHP“rawurlencode”可能有用?@RyanVincent但我的问题是,我需要以某种方式将JSON对象传递到另一台服务器上,而不是从我创建它的地方,因此我需要以某种方式定义一个URL,我需要将该对象发布到另一台不会使用PHP进行解码的服务器上。是否可以使用某种东西发送准确的对象?类似于
$.post(“url”,$json_输出)
?如何集成php和jquery/ajax?然后您可以使用
JSON.stringify(JSON_数组)
将对象转换为字符串,您可以轻松地发送它并使用
JSON.parse(jsonString)
解码..但是如何将PHP对象传递给jquery?在脚本文件
var json\u array=
中,我需要将该对象发送到另一个不使用PHP解码的服务器。是否可以使用某种东西发送准确的对象?类似于
$.post(“url”,$json_输出)
?如何集成php和jquery/ajax?然后您可以使用
JSON.stringify(JSON_数组)
将对象转换为字符串,您可以轻松地发送它并使用
JSON.parse(jsonString)
解码..但是如何将PHP对象传递给jquery呢?在您的脚本文件
var json\u array=
非常感谢您的回答,我在执行时遇到以下错误:
PHP致命错误:调用未定义的函数curl\u init()
你知道如何摆脱它吗?@TonyBorcello你需要安装php curl libary。sudo apt get安装php5 curl sudo服务apache2restart@TonyBorcello如何安装curl非常感谢您的回答,我在执行时遇到了以下错误:
PHP致命错误:调用未定义的函数curl\u init()
有什么想法吗?@TonyBorcello您需要安装PHP curl libary。sudo apt get安装php5 curl sudo服务apache2restart@TonyBorcello如何安装curl