【问题标题】:Why curl not working when I post some values?为什么当我发布一些值时 curl 不起作用?
【发布时间】:2014-01-21 07:22:30
【问题描述】:

我正在尝试使用 curl 将一些值发布到 URL

//set up a connection variable for the page you will post data to
$curl_connection = curl_init('https://payment.teqwerty.com/NetBanking/Pay.jsp?');
 
//curl basic setup
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
 
//$_POST variables to pass
$post_items[] = 'MerchantId='.strip_tags($merchantId);
$post_items[] = 'Password='.strip_tags($Password);
$post_items[] = 'RemoteIP='.strip_tags($RemoteIP);
$post_items[] = 'Amount='.strip_tags($Amount);
$post_items[] = 'BankId='.strip_tags($BankId);
$post_items[] = 'Checksum='.strip_tags($checksum);
$post_items[] = 'Name='.strip_tags($Name);
$post_items[] = 'mobileNo='.strip_tags($mobileNo);
$post_items[] = 'Email='.strip_tags($Email);

 
//format the $post_items into a string
$post_string = implode ('&', $post_items);
 
//send the $_POST data to the new page
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($curl_connection);
curl_close($curl_connection);

我是 curl 的新手,这段代码是我从谷歌搜索的。但这似乎不起作用。但我有一个工作代码

$parameters='MerchantId='.$merchantId.'&Password='.$Password.'&ReferenceNo='.$ReferenceNo.'&RemoteIP='.$RemoteIP.'&Amount='.$Amount.'&BankId='.$BankId.'&Checksum='.$checksum.'&Name='.$Name.'&MobileNo='.$MobileNo.'&Email='.$Email;
    //echo "para is <br>".$parameters;
    //set POST variables
    $url = 'https://payment.teqwerty.com/NetBanking/Pay.jsp?'.$parameters; 
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $auth = curl_exec($curl);

以上工作正常。但奇怪的是,有时这会卡住并且不起作用。我想将参数发布到该 URL。无论如何,我不会得到任何变量作为回报。为什么第一个代码不起作用。我做错了吗?

【问题讨论】:

  • 尝试发布 url "payment.teqwerty.com/NetBanking/Pay.jsp" 然后 $postfields 和 curl_setopt($curl_connection, CURLOPT_POST,1);
  • 当第二种方法有效时为什么不使用它?
  • @ShankarDamodaran 它正在工作,但有时会卡住。我无法弄清楚原因。有时它工作正常

标签: php html curl


【解决方案1】:

这个答案是关于下面的评论..

@ShankarDamodaran.. 它正在工作,但有时会卡住。我 无法弄清楚原因。有时它可以正常工作

那肯定是url编码问题。

这样做..

$parameters='MerchantId='.$merchantId.'&Password='.$Password.'&ReferenceNo='.$ReferenceNo.'&RemoteIP='.$RemoteIP.'&Amount='.$Amount.'&BankId='.$BankId.'&Checksum='.$checksum.'&Name='.$Name.'&MobileNo='.$MobileNo.'&Email='.$Email
$parameters = urlencode($parameters); //<--- URL Encode it before sending to the cURL 

【讨论】:

  • 好的会检查并通知你
  • 编码它不起作用。它在 api 方面给我错误,因为我发布的格式不正确。当前的现在正在工作。但不能相信它,因为它有时不。为什么它有时会卡住的任何其他可能性?
  • 其实我的代码有错别字...我错过了$parameters 变量中的estuck 到底是什么意思?你能解释一下吗?
  • 我已经删除了错字,现在不会出现错误了。我会解释一下,有一个表格可以收集所有要发送到网址的详细信息(参数)。它实际上是一个付款表格.用户填写,然后点击支付。然后他把这个php作为表单动作。他在表单中填写的值将被保存到变量中并使用curl,将通过curl发布到上面的url。实际上它工作正常,但我发现我的一些客户有问题,在提交表格后,当他们点击付款时,php 没有加载,它是空白的(而不是将它们带到 3D 安全页面,它应该在 curl 发布后做什么)
【解决方案2】:

因为,您阻止 SSL 验证为假。 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 然后,我想您还必须提及 SSL 版本,即 curl_setopt($init_curl, CURLOPT_SSLVERSION, 3);

这只是一个想法。因为我以前也面临过这样的问题。然后,我提到了 SSL 版本,它运行良好..

【讨论】:

    【解决方案3】:

    您应该只有一种发送 http 数据 POST 或 GET 的方式。第一种方法使用 POST,而第二种方法使用 GET。正如您所说,第一种方法不起作用,因此很明显,您的请求必须仅作为 GET 请求发送。

    对于有时不工作的问题,您必须检查响应头/数据以获取更多信息。

    仅检查标题使用

    curl_setopt($ch, CURLOPT_HEADER, 1);
    
    $headers = curl_getinfo($ch);
    

    【讨论】:

      猜你喜欢
      • 2021-11-28
      • 1970-01-01
      • 2019-10-26
      • 2019-06-04
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      相关资源
      最近更新 更多