1

I had a case, I need to post data to API end point that has redirect response, i mean when i hit for example "xxx.com/api/v1/receive" with several post parameters, its suppose to redirect to a page that handle authentication provided by API provider for further access, then redirect back to my site.

in my code i implement something like this to post data

$data = ['key'=>'val'.etc...];
$post_data = http_build_query($data);

// open connection
$ch = curl_init();

// set option
curl_setopt($ch,CURLOPT_URL,$this->dev_url);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch,CURLOPT_FRESH_CONNECT,true);


// execute
$result = curl_exec($ch);

when i use regular form submit using html form and post some parameters its run with redirect stuff like its suppose to do , but when i post data like my code above, it has no response. Is it possible to get response (redirect response) like html form using curl?

Alexander Guz
  • 1,334
  • 12
  • 31

1 Answers1

0
$data = ['key'=>'val'.etc...];

$result = file_get_contents ( "http://yoururl.com", false, stream_context_create ( array (
                    'http' => array (
                            'method' => 'POST',
                            'content' => json_encode ( $data ) 
                    ))));

you can check the var_dump($result);

Gireesh T
  • 57
  • 1
  • 9