cURL request in Laravel

前端 未结 5 1333
梦如初夏
梦如初夏 2021-01-30 23:44

I am struggling to make this cURL request in Laravel

curl -d \'{\"key1\":\"value1\", \"key2\":\"value2\"}\' -H \"Content-Type: application/json\"   -X GET http:/         


        
5条回答
  •  甜味超标
    2021-01-31 00:17

    Give the query-option from Guzzle a try:

    $endpoint = "http://my.domain.com/test.php";
    $client = new \GuzzleHttp\Client();
    $id = 5;
    $value = "ABC";
    
    $response = $client->request('GET', $endpoint, ['query' => [
        'key1' => $id, 
        'key2' => $value,
    ]]);
    
    // url will be: http://my.domain.com/test.php?key1=5&key2=ABC;
    
    $statusCode = $response->getStatusCode();
    $content = $response->getBody();
    
    // or when your server returns json
    // $content = json_decode($response->getBody(), true);
    

    I use this option to build my get-requests with guzzle. In combination with json_decode($json_values, true) you can transform json to a php-array.

提交回复
热议问题