php发送json请求
我正在开发API,在API中我需要对url进行post调用 身体是php发送json请求,php,json,curl,Php,Json,Curl,我正在开发API,在API中我需要对url进行post调用 身体是 { "image":"http://media.kairos.com/kairos-elizabeth.jpg", "subject_id":"subtest1", "gallery_name":"gallerytest1", "selector":"SETPOSE", "symmetricFill":"true" } 标题是 'Content-Type:application/json', 'app_id:app_id'
{
"image":"http://media.kairos.com/kairos-elizabeth.jpg",
"subject_id":"subtest1",
"gallery_name":"gallerytest1",
"selector":"SETPOSE",
"symmetricFill":"true"
}
标题是
'Content-Type:application/json',
'app_id:app_id',
'app_key:app_key'
我正在使用curl。下面是代码
<?php
$ch = curl_init('some url');
$content= array(
'image'=>'some_image.jpg',
'subject_id'=>'subtest1',
'gallery_name'=>'gallerytest1',
'selector'=>'SETPOSE',
'symmetricFill'=>'true'
);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type:application/json',
'app_id:id',
'app_key:key'
),
CURLOPT_POSTFIELDS => json_encode($content)
));
$response = curl_exec($ch);
if($response === FALSE){
die(curl_error($ch));
}
$responseData = json_decode($response, TRUE);
?>
当我运行脚本时,会出现空白页。请帮助我查找问题。请尝试我的源代码bro
$data_string = array(
'image' =>'some_image.jpg',
'subject_id' =>'subtest1',
'gallery_name' =>'gallerytest1',
'selector' =>'SETPOSE',
'symmetricFill' =>'true'
);
$data_string = json_encode($data_string);
$ch = curl_init('http://localhost/testGuzzle/jsOnChange.php');
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),
'app_id:id',
'app_key:key'
));
$result = curl_exec($ch);
if($result === FALSE){
die(curl_error($ch));
}else{
echo $result;
}
还有我的听众来源
ini_set("allow_url_fopen", true);
$jsonStr = file_get_contents("php://input"); //read the HTTP body.
var_dump($jsonStr);
echo "<br>";
$json = json_decode($jsonStr, true);
print_r($json);
ini\u集(“允许url\u fopen”,true);
$jsonStr=文件获取内容(“php://input"); //阅读HTTP正文。
var_dump($jsonStr);
回声“
”;
$json=json_decode($jsonStr,true);
打印(json);
和结果
希望这有帮助!!!xD试试我的源代码兄弟
$data_string = array(
'image' =>'some_image.jpg',
'subject_id' =>'subtest1',
'gallery_name' =>'gallerytest1',
'selector' =>'SETPOSE',
'symmetricFill' =>'true'
);
$data_string = json_encode($data_string);
$ch = curl_init('http://localhost/testGuzzle/jsOnChange.php');
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),
'app_id:id',
'app_key:key'
));
$result = curl_exec($ch);
if($result === FALSE){
die(curl_error($ch));
}else{
echo $result;
}
还有我的听众来源
ini_set("allow_url_fopen", true);
$jsonStr = file_get_contents("php://input"); //read the HTTP body.
var_dump($jsonStr);
echo "<br>";
$json = json_decode($jsonStr, true);
print_r($json);
ini\u集(“允许url\u fopen”,true);
$jsonStr=文件获取内容(“php://input"); //阅读HTTP正文。
var_dump($jsonStr);
回声“
”;
$json=json_decode($jsonStr,true);
打印(json);
和结果
希望这有帮助!!!xD
错误报告(E\u ALL);ini_集('display_errors','1')代码>我尝试了这个仍然空白的页面。空白页面通常意味着您的PHP语法有错误。您应该,或者查看您的服务器错误日志。var\u dump($response)代码>抱歉,我在ini_集合(“显示错误”,“1”)中使用了1而不是“1”<代码>错误报告(E_全部);ini_集('display_errors','1')代码>我尝试了这个仍然空白的页面。空白页面通常意味着您的PHP语法有错误。您应该,或者查看您的服务器错误日志。var\u dump($response)代码>抱歉,我在ini_集合(“显示错误”,“1”)中使用了1而不是“1”;