[英]Php post http request using json
嗨,我正在嘗試使用POST方法發送此json對象,但我不確定如何包含下面的標頭,我正在檢查很多示例,但沒有人使用下面列出的標頭。
這是我的php處理頁面的代碼,該頁面將處理從form.html接收的輸入,並將創建json對象以傳輸到http服務器(外部平台)
<?php
$host ="10.10.237.8";
$puerto = 21098;
$BUFF = 2048;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$conexion = socket_connect($socket, $host, $puerto);
if($conexion)
{
$msisdn=$_POST["msisdn"];
$mensaje=$_POST["mensaje"];
$php_array = array(
"numero" => $msisdn,
"mensaje" => "$mensaje",
);
$json_array=json_encode($php_array);
echo "Conexion Exitosa, puerto: " . $puerto."\n\n";
echo $json_array;
socket_write($socket, $json_array);
// Receive the results (if any) from the server
$server_response = socket_read($socket, $BUFF);
$decoded_response = json_decode($server_response, true); // decode the data received
echo $decoded_response['passphrase']; // and spit the results
}
else
{
echo "\nLa conexion TCP no se pudo realizar, puerto: ".$puerto;
}
socket_close($socket);
?>
這些是標題信息,我應該在哪里包含它們? 怎么做 ? 使用cURL嗎? 你有一個很好的例子嗎?
POST /SMBULK/BATCH HTTP/1.0
Authorization: Basic dGVzdDp0ZXN0
Host: 10.10.237.8:21098
Content-Length: 395
User-Agent: Wget/1.12 (solaris2.10)
Content-Type: application/x-www-form-urlencoded
Connection: Keep-Alive
Accept: */*
我希望你能幫助我。 上面的php代碼不起作用,因為其中未包含某些信息(POST,Content-Length,Content-Type,User-Agent ...等)(“ host”除外)。
我設法做到了,但是沒有用:
<?php
$str_obj_json='{
"method":"SUBMIT","params":{
"batchType":"submit",
"batchId":"alvarons",
"origAddr":"550",
"origTon":2,
"userData":"Movistar les desea Feliz Navidad",
"submits":
[
{
"messId":"mess127_001",
"destAddr":"51975375377"}
]
}
}';
$ch = curl_init('http://10.10.237.8:21098/SMBULK/BATCH');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $str_obj_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: 395',
'Authorization: Basic dGVzdDp0ZXN0',
'User-Agent: Wget/1.12 (solaris2.10)',
'Connection: Keep-Alive',
'Accept: */*')
);
$result = curl_exec($ch);
?>
怎么了 ?
要使用cUrl發送json對象,您必須在標頭中添加“ Content-Type:application / json”
curl_setopt($curl, CURLOPT_HTTPHEADER,
array(
'Content-type: application/json'
'Content-Length: 395',
'Authorization: Basic dGVzdDp0ZXN0',
'User-Agent: Wget/1.12 (solaris2.10)',
'Connection: Keep-Alive',
'Accept: */*');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.