Pass whole incoming data to curl – Laravel

I wanted to pass the whole incoming data (that is, $request) to the curl not wanted to post to a particular field in the endpoint as subjectId=>1 as am running this curl request for different endPoint everytime. The below curl request will work if CURLOPT_URL => $url . $subjectId, was given. As my input changes for every end point, i’ve to pass everything that comes in the input to the curl , i can’t pass it as an arary $subjectId. Is there any way to do this?

Currently, dd($Response); returns null

Am giving a postman input like this:

{
   "subjectId":"1"
}

Curl
public function getContentqApiPost(Request $request) 
    {
        $token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.ey";
        $headers = [
            "Accept: application/json",
            "Authorization: Bearer " . $token
        ];
       
          $url="http://127.0.0.1:9000/api/courses/course-per-subject";
        $subjectId = "?subjectId=$request->subjectId";
        $ch = curl_init();
        $curlConfig = array(
         //   CURLOPT_URL            => $url . $subjectId,
         CURLOPT_URL            => $url . $request,
            
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_RETURNTRANSFER => true,
            
            CURLOPT_HTTPHEADER => $headers, 
        );
         
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt_array($ch, $curlConfig);
        
        $result = trim(curl_exec($ch));
       
        $Response = json_decode($result, true);
        
        if (curl_errno($ch)) {
            $error_msg = curl_error($ch);
            echo $error_msg;
        }

        curl_close($ch);

        return $Response;
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you would like to pass all params of $request to curl:

$queryParams  = '';
$delimeter = '?';
foreach($request->all() as $k => $v){
    $queryParams .= "$delimeter$k=$v";
    $delimeter = '&';
}

Also You can only pass the params you want:
foreach($request->only(['subjectId']) as $k => $v){
    // code here
}

Finally you have:
CURLOPT_URL            => $url . $queryParams,

Method 2

Answer

Assuming you want to pass the entire GET query string as-is:

$query_string = str_replace($request->url(), "", $request->fullUrl());
$url = "http://localhost:9000/api/courses/course-per-subject" . $query_string;

This works because $request->url() returns the URL without the query string parameters, while $request->fullUrl() returns the URL with all the query string parameters, so we can use str_replace with an empty replacement to remove the non-query part. Note that $query_string will already start with a ? so there is no need to add that yourself.


Other suggestions

Unless your Laravel API is a 1:1 copy of the backend API, I strongly suggest writing a class that interfaces with the backend API, then provide it to your Laravel controllers using dependency injection. E.g.

class CourseCatalogApi {
   public function getSubjectsInCourse(String $course){
       ... // your curl code here
   }
}

Finally, since you are already using Laravel, there is no need to write such low level code using curl to make HTTP requests. Consider using guzzlehttp, which is already a dependency of Laravel.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x