donglian4464 2019-04-22 10:16
浏览 62

参数不使用Php传递卷曲

I am trying to fetch data from webservice(nodejs) using curl in php but i am getting result "lang is required" i tried with following code but not working for me where i am wrong ? Here is my code

$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
  $ch = curl_init();
  $url="http://xxxxxx:8000/api/employer/login";
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  $response = curl_exec($ch);
  $result = json_decode($response);
  echo "<pre>";print_R($result);
  • 写回答

2条回答

  • doushichun9409 2019-04-22 10:28
    关注

    Usually to send post variables i use : the http_build_query function.

    $url = "http://xxxxxx:8000/api/employer/login";
    $post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    
    $response = curl_exec($ch);
    $result = json_decode($response);
    echo "<pre>";print_R($result);
    

    It works better.

    评论

报告相同问题?