doujia9204 2017-07-24 02:15
浏览 53

用于响应curl post请求的php代码

I am new to cURL and I am setting up a test environment to send a cURL POST request using PHP and then I need to mimic the server (responding) program (PHP also) with a status code and payload. After looking and reading up on cURL and corresponding documentation and pertinent RFC documents there are a lot of examples on the request side but I could find no pertinent examples for the response side. My request code is as follows:

$url = 'http://localhost/curlresponder.php';  
              $fields =  array(
                   'City'            => urlencode('Hokeywaka'),
                   'State'           => urlencode('IL'),
                   'Zip'             => urlencode('60677'),
              );
              foreach($fields as $key => $value)
                     {$fields_string .= $key . "=" . $value . '&';}
                     rtrim($fields_string, '&');

              $ch = curl_init();

              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_POST, TRUE);
              curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

              $result = curl_exec($ch);

              curl_close($ch);
              if  ($result === FALSE)
                  {echo 'Return failure ' . '<br />';
              else
                  {Process $result;}

Now I need to know what the responding program code is - assuming the request is received and processed successfully the response code of 200 is to be sent back with a payload attached (which I gather is also POST since the request was POST.

  • 写回答

2条回答 默认 最新

  • douchong4730 2017-07-24 02:30
    关注

    You just need the remote page to post back a JSON script. And by Post back I just mean echo a JSON string on the page. You can use that in your $result var. You might want to create an API access key just to make sure nobody else uses it if you think there might be a security issue.

    $result = curl_exec($ch);
    $result = json_decode($result,true);
    if ($result['status']=="ok"){
        echo "Good request";
    }else{
        echo "Bad request";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路