2

I tried to send JSON request from me server like this :

public function subscribe() {
    $arrData = array(
        'name' => $name,
        'user_id' => $user_id,
        'merchant_reference' => $merchant_reference
    );

    $ch = curl_init(env('MY_API'));
    # Setup request to send json via POST.
    $data = json_encode($arrData);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    # Return response instead of printing.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    # Send request.
    $result = curl_exec($ch);
    curl_close($ch);
}

And when I call this function from the browser I get error 415, But when I call it from Postman it work's fine and return the right response.

Any ideas?

18
  • Where is $arrData defined? Commented Sep 27, 2016 at 11:18
  • add 'Accept: application/json', in header and test, and # Setup request to send json via POST. how? Commented Sep 27, 2016 at 11:18
  • $arrData does not exist in the subscribe method's scope. Commented Sep 27, 2016 at 11:22
  • I added Accept: application/json header but still getting the same error 415 Unsupported Media Type Commented Sep 27, 2016 at 11:24
  • have you tried to convert post json to curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); ? Commented Sep 27, 2016 at 11:29

3 Answers 3

1

The problem is like what @devpro said in my local server, it seems that there is some configuration that i need to change in my local server to allow sending JSON request

Sign up to request clarification or add additional context in comments.

Comments

0

As I see you don't pass $arrData to the subscribe function

1 Comment

The $arrData is inside subscribe function
0

You forgot set option

curl_setopt($ch, CURLOPT_POST, true);

Also always can debug query with any own TCP listener, like a netcat

UPD: After send request, check response status code:

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

1 Comment

@MohammadAL-Raoosh, your code, not consider lack of CURLOPT_URL option, sucessful run. So, maybe 415 not related with this query?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.