Minggu, 01 Juni 2014

namespace pada PHP

namespace adalah teknologi script baru pada php yang ada pada php versi 5.0 > 5.3.0. Apa itu namespace ? Sesuai dengan namanya, namespace adalah ruang nama sebagai contoh, dalam setiap direktori sistem operasi berfungsi untuk mengelompokan file terkait, dan bertindak sebagai namespace untuk file-file dalam diri mereka. berikut adalah contoh script namespace dalam php.

ns1.php:
<?php 
namespace pertama;
  class 
satu {
    static function 
jalankan() {echo 'ini namespace pertama';}  } 
?>


ns2:
<?php 
namespace kedua;
  class 
dua {
    static function 
jalankan() {echo 'ini namespace kedua';}  } 
?>


ns3:
<?php 
namespace ketiga;
  class 
tiga {
    static function 
jalankan() {echo 'ini namespace pertama';}  } 
?>


index.php:
<?php 
namespace test;
  include 
'ns1.php';
  include 
'ns2.php';
  include 
'ns3.php';
  use 
pertama as panggil1;
  use 
kedua as panggil2;
  use 
ketiga;
  echo 
panggil1satu::jalankan(), "<br />\n";
  echo 
panggil2dua::jalankan(), "<br />\n";
  echo 
ketigatiga::jalankan(), "<br />\n";  
?>

Trait php

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and classes is defined in a way which reduces complexity, and avoids the typical problems associated with multiple inheritance and Mixins.

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,