Rabu, 26 Februari 2014

post json data via php 1

Post json,

<?php
$ch = curl_init();
$timeout = 10; // set to zero for no timeout

$urltouse = "http://dpdev-ld03.myservercom:8181/linkmethod/signup?f=json&c=signupcallback
";

$fields_string = '{siteUrl:\"http://testurl.com\",siteRss:\"http://feeds.news.mysite.com/synfeeds/collect/6/rss.xml
\",userName:\"testuser\",email:\"testemail\",siteName:\"testsitename
\",loginId:\"testloginid\}';

curl_setopt($ch,CURLOPT_URL,$urltouse);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); <-- shouldn't be
postfields, but some other curl option I'm guessing

$response = curl_exec($ch);
curl_close($ch);

echo $response;

?>



curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query(array("data"=>$fields_string)));

Now on the server end $_POST['data'] will contain your json data.

if you just pass the $fields_string it can be read in the server end too.
$data = file_get_contents("php://input");
now $data will contain your json data.

It completely depends on the server you are interacting with. If it
needs json in a variable then pass it using http_build_query.
Otherwise just pass json as you are doing right now.

post json data via php

Saya punya partner dari Afrika, mereka minta data dari kami  di kirim ke mereka dengan format json dan dennga method Post.


Dari sisi client,
 $url = 'http://localhost/koin/hd/mo_recv_post.php?';
    $post = array(
    "product_id" => 12345,
    "message" => "tes tes",
    "transaction_id" => "38279429429",
    "msisdn" => "628569876543256",
    "program_id" => "hd1hr",
    "datetime" => date('Y-m-d h:i:s')
    );
   
    $postText = http_build_query($post);
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postText);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    $result = curl_exec($ch);
    $status = curl_getinfo($ch);
    curl_close($ch);

Disisi server,

tinggal di get post saja $_POST
ini codingnya,