【问题标题】:How to call other url using php..?如何使用 php 调用其他 url ..?
【发布时间】:2017-05-19 09:22:41
【问题描述】:

当我使用邮递员应用程序点击以下网址时,它工作正常

http://192.168.1.220/cgi-bin/handle_login.tcl

但是当尝试使用 curl 时,它给了我以下错误

    The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

下面是我的php代码

    <?php
   $postData = array(
   'user' => 'admin',
   'pw' => 'admin',
   'submit' => 'Login'
   );

  // Setup cURL
  $ch = curl_init('http://192.168.1.220/cgi-bin/handle_login.tcl');
  curl_setopt_array($ch, array(
  CURLOPT_POST => TRUE,
  CURLOPT_RETURNTRANSFER => TRUE,
  CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'
 ),
  CURLOPT_POSTFIELDS => json_encode($postData)
));

 // Send the request
   $response = curl_exec($ch);

 var_dump($response);

 // Check for errors
  if($response === FALSE){
  die(curl_error($ch));
  }

   // Decode the response
    $responseData = json_decode($response, TRUE);

// Print the date from the response
 echo $responseData['published'];
 ?>

【问题讨论】:

  • 您似乎严重误解了 Stack Overflow 的工作原理。请查看help center,主要是How to AskBe Nice
  • @Nitish..u 认为我发布了支持投票的问题 ....在以前的问题之一中,用户心甘情愿地投票并发布了一些不相关的答案,并要求我进行绿色勾号和支持投票...我不这样做..我面临错误我发布了我得到的东西......如果你想帮助我,只需通知我我所做的错误......
  • 大声笑...@Nitish ....我想你一直在通过我的帖子....我认为我自己发布了答案......

标签: php web-services curl


【解决方案1】:

我已经纠正了我的代码中的错误并得到了想要的答案...

以下是答案

    $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL,"URL");
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS,
    "key1=value1&key2=value2&key3=value3");         
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Send the request 
     $response = curl_exec($ch);
     curl_close ($ch);
     echo "$response";

【讨论】:

  • 我不反对这篇文章。但对我来说,上面的代码运行良好......
猜你喜欢
  • 2019-12-29
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多