php.blade.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. @php
  2. use Knuckles\Scribe\Tools\WritingUtils as u;
  3. /** @var Knuckles\Camel\Output\OutputEndpointData $endpoint */
  4. @endphp
  5. <pre><code class="language-php">
  6. $client = new \GuzzleHttp\Client();
  7. @if($endpoint->hasRequestOptions())
  8. $response = $client->{{ strtolower($endpoint->httpMethods[0]) }}(
  9. '{{ rtrim($baseUrl, '/') . '/' . ltrim($endpoint->boundUri, '/') }}',
  10. [
  11. @if(!empty($endpoint->headers))@php
  12. // We don't need the Content-Type header because Guzzle sets it automatically when you use json or multipart.
  13. unset($endpoint->headers['Content-Type']);
  14. @endphp
  15. 'headers' => {!! u::printPhpValue($endpoint->headers, 8) !!},
  16. @endif
  17. @if(!empty($endpoint->cleanQueryParameters))
  18. 'query' => {!! u::printQueryParamsAsKeyValue($endpoint->cleanQueryParameters, "'", "=>", 12, "[]", 8) !!},
  19. @endif
  20. @if($endpoint->hasFiles())
  21. 'multipart' => [
  22. @foreach($endpoint->cleanBodyParameters as $parameter => $value)
  23. @foreach(u::getParameterNamesAndValuesForFormData($parameter, $value) as $key => $actualValue)
  24. [
  25. 'name' => '{!! $key !!}',
  26. 'contents' => '{!! $actualValue !!}'
  27. ],
  28. @endforeach
  29. @endforeach
  30. @foreach($endpoint->fileParameters as $parameter => $value)
  31. @foreach(u::getParameterNamesAndValuesForFormData($parameter, $value) as $key => $file)
  32. [
  33. 'name' => '{!! $key !!}',
  34. 'contents' => fopen('{!! $file->path() !!}', 'r')
  35. ],
  36. @endforeach
  37. @endforeach
  38. ],
  39. @elseif(!empty($endpoint->cleanBodyParameters))
  40. 'json' => {!! u::printPhpValue($endpoint->cleanBodyParameters, 8) !!},
  41. @endif
  42. ]
  43. );
  44. @else
  45. $response = $client->{{ strtolower($endpoint->httpMethods[0]) }}('{{ rtrim($baseUrl, '/') . '/' . ltrim($endpoint->boundUri, '/') }}');
  46. @endif
  47. $body = $response->getBody();
  48. print_r(json_decode((string) $body));
  49. </code></pre>