php - Sending data to a webservice using post -


i've been provided example of how connect webserver.

it's simple form 2 inputs:

webservice form

it returns true after submitting form both token , json.

this code:

<!doctype html> <html lang="en">     <head>         <meta charset="utf-8" />         <title>webservice json</title>     </head>     <body>         <form action="http://foowebservice.com/post.wd" method="post">             <p>                 <label for="from">token: </label>                 <input type="text" id="token" name="token"><br>                 <label for="json">json: </label>                 <input type="text" id="json" name="json"><br>                 <input type="submit" value="send">                 <input type="reset">             </p>         </form>     </body> </html> 

in order make dynamic, i'm trying replicate using php.

$url = "http://foowebservice.com/post.wd";  $data = array(   'token' => 'footoken',   'json' => '{"foo":"test"}', );  $content = json_encode($data); $curl = curl_init($url); curl_setopt($curl, curlopt_header, false); curl_setopt($curl, curlopt_returntransfer, true); curl_setopt($curl, curlopt_httpheader,   array("content-type: application/json")); curl_setopt($curl, curlopt_post, true); curl_setopt($curl, curlopt_postfields, $content);  $json_response = curl_exec($curl);  $status = curl_getinfo($curl, curlinfo_http_code);  curl_close($curl);  $response = json_decode($json_response, true); 

but must doing wrong, because $response it's given false value.

i don't mind doing in other way instead of using curl.

any suggestions?

update

as suggested in first answer, i've tried set $data array in way:

$data = array(   'token' => 'footoken',   'json' => array('foo'=>'test'), ); 

however response false.

i've tried postman rest - client plugin chrome, , using development tools / network, url in headers is:

request url:http://foowebservice.com/post.wd?token=footoken&json={%22foo%22:%22test%22} 

which assume same url should sent using curl.

you're passing post data in json format, try pass in form k1=v1&k2=v2. example, add following after $data array definition:

foreach($data $key=>$value) { $content .= $key.'='.$value.'&'; } 

then remove following lines:

$content = json_encode($data); 

and

curl_setopt($curl, curlopt_httpheader,    array("content-type: application/json")); 

complete code (tested):

test.php

<? $url = "http://localhost/testaction.php";  $data = array(   'token' => 'footoken',   'json' => '{"foo":"test"}', );  foreach($data $key=>$value) { $content .= $key.'='.$value.'&'; }  $curl = curl_init($url); curl_setopt($curl, curlopt_header, false); curl_setopt($curl, curlopt_returntransfer, true); curl_setopt($curl, curlopt_post, true); curl_setopt($curl, curlopt_postfields, $content);  $json_response = curl_exec($curl);  $status = curl_getinfo($curl, curlinfo_http_code);  curl_close($curl);  $response = json_decode($json_response, true); var_dump($response); ?> 

testaction.php

<? echo json_encode($_post); ?> 

output:

array(2) {   'token' =>   string(8) "footoken"   'json' =>   string(14) "{"foo":"test"}" } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -