【问题标题】:curl post data and get resultcurl 发布数据并获取结果
【发布时间】:2015-12-26 13:23:45
【问题描述】:

这是我的代码:

<?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 并在我自己的网站上获得结果。我怎样才能做到这一点 ?

【问题讨论】:

  • 什么是$result?在$matches 中打印preg_match_all 的输入是什么。预期的产出是什么?
  • 什么是错误的结果?什么应该是正确的?

标签: php arrays regex curl


【解决方案1】:

某些网页需要整个帖子数据,无论该值是否为空。一个名为 http_build_query() 的函数可以轻松构建帖子或获取查询字符串,您只需提供参数类型数组即可。

【讨论】:

    【解决方案2】:
    $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";
             }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      相关资源
      最近更新 更多