나는이 데이터를 가지고있다 :
{
userID: 'a7664093-502e-4d2b-bf30-25a2b26d6021',
itemKind: 0,
value: 1,
description: 'Boa saudaÁ„o.',
itemID: '03e76d0a-8bab-11e0-8250-000c29b481aa'
}
그리고 json url에 게시해야합니다 : http : // domain/OnLeagueRest/resources/onleague/Account/CreditAccount
pHP를 사용 하여이 게시물 요청을 어떻게 보낼 수 있습니까?
외부 의존성 또는 라이브러리를 사용하지 않고 없이 :
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n"
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
$ response 는 객체입니다. 평소와 같이 속성에 액세스 할 수 있습니다 (예 : $ 응답-> ...
여기서 $ data 는 데이터를 포함하는 배열입니다.
$data = array(
'userID' => 'a7664093-502e-4d2b-bf30-25a2b26d6021',
'itemKind' => 0,
'value' => 1,
'description' => 'Boa saudaÁ„o.',
'itemID' => '03e76d0a-8bab-11e0-8250-000c29b481aa'
);
경고 : allow_url_fopen 설정이 다음으로 설정되면 작동하지 않습니다. php.ini에서 Off .
WordPress 를 개발하는 경우 제공된 API 사용을 고려하십시오. http://codex.wordpress.org/HTTP_API
이 목적으로 CURL을 사용할 수 있습니다 (예제 코드 참조).
$url = "your url";
$content = json_encode("your data to be sent");
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
cURL luke :)를 진지하게 사용하십시오.이를 수행하는 가장 좋은 방법 중 하나이며 응답을 얻습니다.