Tolong pilih kategori sesuai, jenis posting (pertanyaan atau bukan) dan sertakan tag/topik yang sesuai misal komputer, php, mysql, dll.
Promosi atau posting tidak pada tempatnya akan kami hapus.
Mencantumkan kode program di posting Anda, tolong ikuti aturan yang sesuai, baca http://diskusiweb.com/discussion/39204/aturan-cara-menyisipkan-kode-program-di-diskusiweb

Baca cara posting gambar/image di post Anda: http://www.diskusiweb.com/discussion/47345/cara-menyisipkan-menyertakan-image-pada-posting/p1

looping cara kirim json dengan url

Selamat sore mas dan mba,

saya ingin menanyakan bagaimana caranya mengirimkan json ke url dari database menggunakan looping. Saya telah membuat script tapi yang terkirim hanya record pertama saja. Tolong mas dan mba memberikan solusi dimana letak kesalahan saya.
Berikut script yang saya buat:
<pre lang="php">
<?php

$hitsms = mysql_query("select count(id) as jml from sentsms");
$hslhitsms = mysql_fetch_array($hitsms);
$jmlkirim = $hslhitsms['jml'];

for($w=1; $w <= $jmlkirim; $w++)
{
$bacano = mysql_query("select no_hp from sentsms where id = '$w'");
$hslbacano = mysql_fetch_array($bacano);
$sentno = $hslbacano['no_hp'];


$url = "http://103.xxx.xxx.xxx/xxxx/sendjson.php";


$rows[] = array (

"hp" => "$sentno",
"message" => "tes kirim json"
);
$content = json_encode($rows);

$curl = curl_init();

curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_HEADER, FALSE );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt( $curl, CURLOPT_POST, TRUE );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $content );
curl_setopt( $curl, CURLOPT_TIMEOUT, 120 );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE );

$htm = curl_exec($curl);

curl_close($curl);

if ($htm)
{
echo "<pre>";
echo $htm;
// DECODE
$json = json_decode($htm,true);
Print_r($json);
}
}
?>
< /pre>
Terima kasih atas bantuannya, maklum newbie tentang json :)


Tanggapan

  • Di kodingan lu , tag close loopingan 'for' emang gk ada ?

    Sudah coba curl_close di taruh setelah loop.. ?
  • edited November 2016
    kakak sacredbluerose
    untuk tag close looping lupa di pastein (sudah saya benerin diatas), hehehe
    kalau curl_close di taruh di luar looping sudah saya coba..tetap saja yang dikirim record pertama saja. Jika di dalam tabel ada 5 record akan terkirim sebanyak 5 kali (isinya record prtama semua)
  • skip/remove bagian Curl .. jalankan kodingan tanpa CURL, dump variable $w dan $hslbacano['no_hp'] !
  • edited November 2016
    jika di hilangkan curlnya bisa looping :

    <pre lang="php">
    <?php

    $hitsms = mysql_query("select count(id) as jml from sentsms");
    $hslhitsms = mysql_fetch_array($hitsms);
    $jmlkirim = $hslhitsms['jml'];

    for($w=1; $w <= $jmlkirim; $w++)
    {
        $bacano = mysql_query("select no_hp from sentsms where id = '$w'");
        $hslbacano = mysql_fetch_array($bacano);
        $sentno = $hslbacano['no_hp'];
       
       
        $url = "http://103.xxx.xxx.xxx/xxxx/sendjson.php";
       
       
        $rows[] = array (
                   
                    "hp" => "$sentno",
                    "message" => "tes kirim json"
                    );
        $content = json_encode($rows);

       
         
            // DECODE
            $json = json_decode($content,true);
            Print_r($json);
       
    }   
    ?>
    < /pre>

    hasilnya looping :

    Array ( [0] => Array ( [hp] => 085228xxxxxx [message] => tes kirim json ) ) Array ( [0] => Array ( [hp] => 085228xxxxxx [message] => tes kirim json ) [1] => Array ( [hp] => 089646xxxxxx [message] => tes kirim json ) ) Array ( [0] => Array ( [hp] => 085228xxxxxx [message] => tes kirim json ) [1] => Array ( [hp] => 089646xxxxxx [message] => tes kirim json ) [2] => Array ( [hp] => 085811xxxxxx [message] => tes kirim json ) ) 
  • well..
    looping work..
    berarti kemungkinan berikutnya, karena data yg lu kirim salah..
    dump $content..
    syntaksnya pake
       echo "<pre>";print_r($content);

  • Hasilnya tetap record pertama yang terkirim

    <pre lang=php>
    <?php

    $hitsms = mysql_query("select count(id) as jml from sentsms");
    $hslhitsms = mysql_fetch_array($hitsms);
    $jmlkirim = $hslhitsms['jml'];

    for($w=1; $w <= $jmlkirim; $w++)
    {
    $bacano = mysql_query("select no_hp from sentsms where id = '$w'");
    $hslbacano = mysql_fetch_array($bacano);
    $sentno = $hslbacano['no_hp'];


    $url = "http://103.xxx.xxx.xxx/xxxx/sendjson.php";


    $rows[] = array (

    "hp" => "$sentno",
    "message" => "tes kirim json"
    );
    $content = json_encode($rows);

    $curl = curl_init();

    curl_setopt( $curl, CURLOPT_URL, $url );
    curl_setopt( $curl, CURLOPT_HEADER, FALSE );
    curl_setopt( $curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
    curl_setopt( $curl, CURLOPT_POST, TRUE );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, $content );
    curl_setopt( $curl, CURLOPT_TIMEOUT, 120 );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE );

    $htm = curl_exec($curl);

    curl_close($curl);




    // DECODE

    echo "<pre>";
      //$json = json_decode($content,true);
    Print_r($content);

    }
    ?>
    </pre>


  • edited November 2016
    eh.. hehe.. mksd gw hasil dump pake syntax gmn?

    atw lu dump di
    sendjson.php; data json yg di Post dah benar belum?
  • kalau di alamat :$url = "http://103.xxx.xxx.xxx/xxxx/sendjson.php"; saya kurang tahu mas, karena itu pihak ke tiga, tapi jika dikirim 1 persatu bisa terbaca disana.

    jadi klu menurut saya di dumping mereka membaca 1 persatu data dengan ansumsi kita membuka link mereka (http://103.xxx.xxx.xxx/xxxx/sendjson.php ) misal :


    http://103.xxx.xxx.xxx/xxxx/sendjson.php --> data record 1

    begitu pengirim kedua

    http://103.xxx.xxx.xxx/xxxx/sendjson.php --> data record 2

    dst....


Sign In or Register to comment.