Search code examples
javaphpphp-stream-wrappers

PHP; get post string from external url (JAVA)


I need to send string data to an external url which is an API for clients, but I always get the server sending back an error, and then I checked the log in the server which is written in JAVA, it throws an exception

"java.lang.NumberFormatException: For input string: "{"success":false,"errorCode":"500","message":"Unknown Error!","value":null,"totalPageCount":0,"footerValue":null}"

and here's my piece of php code:

$context = stream_context_create(array( 
'http' => array( 
    'method' => 'POST', 
    'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
    'content' => $params  
    ), 
)); 
$received = file_get_contents(URL, false, $context); 
echo $received.'<br>';

I'm pretty sure the data I send to the server is in correct format as I have the access to the log, and I know that all response parameters from the server are in JSON, UTF-8 and post method, is there something I missed?

If more information needed please let me know, thanks.


Solution

  • You need to parse the string. You are getting NumberFormatException and this is because your server is receiving data which is not a numeric.

    Integer.parseInt() // in java
    

    You can parse the content in server side this way or send numeric data.

    Another method:

    Integer.valueOf() // in java