PHP 는 Http Post 를 사용 하여 JSon 대상 데이터 코드 분석 을 요청 합 니 다.
Http 에서 전송 대상 의 가장 좋 은 표현 형식 은 JSON 문자열 입 니 다.그러나 매개 변수의 수신 자로 서 알려 져 야 할 것 은 JSON 입 니 다!
사실 이것 은 어렵 지 않 습 니 다.http Content-Type 헤더 메 시 지 를 보 내 면 됩 니 다.즉,'Content-Type:application/json;charset=utf-8"참조 코드 는 다음 과 같 습 니 다.
<?php
/**
* PHP Json
*
* @param $url url
* @param $jsonStr json
* @return array
*/
function http_post_json($url, $jsonStr)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($jsonStr)
)
);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array($httpCode, $response);
}
$url = "http://52php.cnblogs.com";
$jsonStr = json_encode(array('a' => 1, 'b' => 2, 'c' => 2));
list($returnCode, $returnContent) = http_post_json($url, $jsonStr);
API 서버 에서 클 라 이언 트 가 보 내 온"Content-Type:application/json;charset=utf-8"헤더 정보 후 http body 데이터(즉,JSon 문자열)를 클래스 대상 으로 변환 합 니 다!이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
laravel에 yo에서 angularJs&coffeescript를 사용할 수 있도록 한다.먼저 yo 명령을 사용할 수 있어야하므로 아래에서 설치 global에 설치한 곳에서 laravel의 프로젝트 루트로 이동. 클라이언트 코드를 관리하는 디렉토리를 만들고 이동합니다. 클라이언트 환경 만들기 이것으로 히...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.