php发送json请求

时间:2016-07-19 18:44:52

标签: php json curl

我正在研究API,我需要对网址进行调用。

身体

{
"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.code位于

之下
<?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);
?>

当我运行脚本时出现空白页面。帮助我找到问题。

1 个答案:

答案 0 :(得分:0)

试试我的源代码

$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);

结果

enter image description here

希望这有帮助!!!的xD