개발! 딱 깔끔하고 센스있게!

PHP cURL options CURLOPTuHEADER, CURLOPTuRETURNTRNTRANSFER 충돌인가요? 본문

개발 스크랩 메모/PHP

PHP cURL options CURLOPTuHEADER, CURLOPTuRETURNTRNTRANSFER 충돌인가요?

렉사이 2020. 12. 23. 22:36

나는 CURL과 Php을 사용하여 API 인증을 하고 있다.

이렇게:

$ch = curl_init(); $headers = []; $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_URL, $this->url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $this->body )); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $this->response = json_decode( curl_exec($ch) ); curl_close ($ch); 

응답을 요청하는 주체는 상태 코드를 포함하고 성공을 요청하면 사용자 대상입니다.

이것은 익명 요청이 있어서, 호응의 영패로 되돌아가는 것이다.

내 질문:스크립트의 상술한 응답이 비어 있습니다.

내가 CURLOPT RETURNTRNSFER 선택을 주석한다면 내가 필요한 것을 얻었지만 호응을 받았다는 것은 1이다.

제가 CURLOPTu HEADER 옵션을 주석하면 호응하는 주체만 받습니다.

http 과 https 사이 전환을 시도해봤습니다.

PHP-5.5.27을 사용하고 있습니다.

대답

너는 맹목적으로 코드를 해명하지만 네가 얻은 것은 JSON 이다.

요컨대: 아니야.제목을 포함한 텍스트인데, 뒤에 JSON 이 있을 수도 있다.

비슷한 일을 하다.

$ch = curl_init(); $headers = []; $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_URL, $this->url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $this->body )); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); $info = curl_getinfo($ch); $actualResponseHeaders = (isset($info["header_size"]))?substr($response,0,$info["header_size"]):""; $actualResponse = (isset($info["header_size"]))?substr($response,$info["header_size"]):""; $this->response = json_decode( $actualResponse ); curl_close ($ch); 
Comments