Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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
如何将php中的curl put与json一起使用?_Php_Json_Curl - Fatal编程技术网

如何将php中的curl put与json一起使用?

如何将php中的curl put与json一起使用?,php,json,curl,Php,Json,Curl,我需要将这个“普通”curl字符串转换为php curl。此命令经测试可在shell中工作: curl -XPUT http://foo/monitors/1.json -d "Monitor[Name]=test1" 我看了很多图坦卡蒙和例子,但到目前为止我的尝试都是徒劳的。以下是我最近尝试的: $data = array("Name" => "test1"); $payload = json_encode( array( "Monitor"=> $data ) ); $url

我需要将这个“普通”curl字符串转换为php curl。此命令经测试可在shell中工作:

curl -XPUT http://foo/monitors/1.json -d "Monitor[Name]=test1"
我看了很多图坦卡蒙和例子,但到目前为止我的尝试都是徒劳的。以下是我最近尝试的:

$data = array("Name" => "test1");
$payload = json_encode( array( "Monitor"=> $data ) );
$url = 'hxxp://foo/monitors/1.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch) 
另外,我尝试输入的json格式如下:

{"monitor":{"Monitor":{"Id":"1","Name":"test","Type":"Remote","Funct.....
谢谢


*更新了更多最新信息

相当于您的命令行示例:

    $data_string =""/* your json string goes here like {"monitor":{"Monitor":{"Id":"1","Name":"test","Type":"Remote"}} */                                                                             

    $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);
$payload = "Monitor[Name]=test1";
$url = 'http://localhost/monitors/1.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

由于您的示例没有使用“application/json”作为“Content Type”,但实际上股票“application/x-www-form-urlencoded”值,“data”不是真正的json

请同时参考问题。对不起,我不确定您的意思是什么?有一个问题与您所问的相同,可以帮助您解决问题。问题链接没问题,我已经看了这些,现在尝试了这个,但没有成功:
code
$data=array(“Name”=>“test1”)$有效载荷=json_编码(数组(“监视器”=>$data))$url=''$ch=curl_init();curl_setopt($ch,CURLOPT_URL,$URL);curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'PUT');curl_setopt($ch,CURLOPT_POSTFIELDS,$payload);curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:application/json'));curl_setopt($ch,CURLOPT_RETURNTRANSFER,true)$响应=curl_exec($ch);旋紧($ch)
code
发生了什么事?有错误吗?好的,这可能是一种方式,但我尝试导航的json开始时是这样的:{“监视器”:{“监视器”:{“Id”:“1”,“名称”:“测试”,“类型”:“Remot…”谢谢,但我想你误解了我最初的问题,我只想输入一个值,而不是整个字符串。