卷曲发布数据并获得结果

时间:2015-12-26 13:23:45

标签: php arrays regex curl

这是我的代码:

<?php
$url = 'http://huaweiunlockcalculator.com/v201-huawei-unlock-calculator-v3';
$fields = array('imei' => $_POST['imei']);

//url-ify the data for the POST
$fields_string="";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, 'http://huaweiunlockcalculator.com/v201-huawei-unlock-calculator-v3');
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
$output=preg_match_all("/Unlock \(V[123]\): ([0-9]+)/",$result, $matches);
echo $matches[0];
//print_r($matches[0][2]);echo "<br>";
//print_r($matches[0][1]);echo "<br>";
//print_r($matches[0][0]);echo "<br>";

curl_close($ch);
//close connection

它带来了错误的结果:( 我需要发送IMEI并在我自己的网站上获得结果。我怎样才能做到这一点 ?

2 个答案:

答案 0 :(得分:0)

某些网页需要整个帖子数据是否为null值。一个名为http_build_query()函数的函数可以轻松地构建一个post或get查询字符串,你只需要给出一个参数类型数组。

答案 1 :(得分:0)

$url =  "http://www.example.com/api/WalletVerification";
            $params = array();
            $inputs = array();
            $inputs['rid']=100;//$retailer_id;
            $inputs['merchantid']="B170127CT3D9H2XJAQUYF0L";
            $inputs['amount']=10;//$wallet_raminfo;
            $params =json_encode($inputs);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $result = curl_exec($ch);
           curl_close($ch);
            $output=  json_decode($result);
         //  print_r($output);
         if($output->status=='000'){
             $raminfostrans_id= $output->transactionid;
             $payment_status=TRUE;
         }else{
              $payment_status=false;
              $failure_msg="insuffient balance";
         }
相关问题