2017-05-24 18 views
-1

는 여기에서 읽어 http://www.phplab.info/categories/laravel/consume-external-api-from-laravel-5-using-guzzle-http-clientLaravel 5.3의 guzzle 6에서 어떻게 응답을받을 수 있습니까?

이 같은 시도 :

가 어떻게 응답을 얻을 수

실행
... 
use GuzzleHttp\Client as GuzzleHttpClient; 
use GuzzleHttp\Exception\RequestException; 
... 
public function testApi() 
{ 
    try { 
     $client = new GuzzleHttpClient(); 
     $apiRequest = $client->request('POST', 'https://myshop/api/auth/login', [ 
      // 'query' => ['plain' => 'Ab1L853Z24N'], 
      'Accept' => 'application/json', 
      'Content-Type' => 'application/json', 
      'auth' => ['[email protected]', '1234'],  //If authentication required 
      // 'debug' => true         //If needed to debug 
     ]); 
     $content = json_decode($apiRequest->getBody()->getContents()); 
     dd($content); 
    } catch (RequestException $re) { 
      //For handling exception 
    } 
} 

, 결과가 null? 나는 우체부 확인

, 결과는

작동 : 그것은

가 업데이트 실패, 목구멍

나는 그것이 성공 응답을

를 얻을 우체부에 시도하지만 사용을 시도 우편 배달부에서 단추 코드를 클릭 해 봅니다.

그런 다음 PHP curl을 선택하고 복사합니다. res 이 같은 ULT는 : 그것은 목구멍 사용하는 경우

<?php 

$curl = curl_init(); 

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://myshop/api/auth/login", 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_ENCODING => "", 
    CURLOPT_MAXREDIRS => 10, 
    CURLOPT_TIMEOUT => 30, 
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
    CURLOPT_CUSTOMREQUEST => "POST", 
    CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"email\"\r\n\r\[email protected]\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\n1234\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", 
    CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache", 
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", 
    "postman-token: 1122334455-abcd-edde-aabe-adaddddddddd" 
), 
)); 

$response = curl_exec($curl); 
$err = curl_error($curl); 

curl_close($curl); 

if ($err) { 
    echo "cURL Error #:" . $err; 
} else { 
    echo $response; 
} 

는 컬 PHP, 그

같은 코드를 사용하는 경우는 어떻게 응답을받을 수 있나요?

+0

왜 내 질문 downvote입니까? 실수가 있습니까? –

답변

1

나는 적어도 하나의 문법 실수를 참조해야한다. request() 방법의 세 번째 인수는 다음과 같아야합니다 : 귀하의 경우

$requestContent = [ 
    'headers' = [], 
    'json' = [] 
]; 

그것은있을 수 :

public function testApi() 
{ 
    $requestContent = [ 
     'headers' => [ 
      'Accept' => 'application/json', 
      'Content-Type' => 'application/json' 
     ], 
     'json' => [ 
      'email' => '[email protected]', 
      'password' => '1234', 
      // 'debug' => true 
     ] 
    ]; 

    try { 
     $client = new GuzzleHttpClient(); 

     $apiRequest = $client->request('POST', 'https://myshop/api/auth/login', $requestContent); 

     $response = json_decode($apiRequest->getBody()); 

     dd($response); 
    } catch (RequestException $re) { 
      // For handling exception. 
    } 
} 

예를 form_params를 들어, 다른 매개 변수 대신 데이터에 대한 json있다. the Guzzle documentation을 살펴 보시기 바랍니다.

+0

$ httpContent가 $ requestContent로 변경되어야합니다. –

+0

결과가 비어 있습니다. –

+0

맞습니다. 편집 됨. 아직도 빈 결과를 얻고 있습니까? – louisfischer

0

이것은

$contents = (string) $apiRequest->getBody(); 
+0

결과는 html입니다. 결과는 API 응답 –

+0

이되어야합니까? HTML 응답이 서버에서 반환 된 오류라고 생각합니다. –

+0

우편 배달부에서 시도했지만 작동합니다. 당신은 내 질문을보십시오. 나는 –