【问题标题】:cURL POST to REST ServicecURL POST 到 REST 服务
【发布时间】:2010-09-27 08:51:42
【问题描述】:

我正在尝试使用 PHP cURL 发布到 REST 服务,但我遇到了一些困难(这是我以前从未使用过 cURL 的原因!!)。

我整理了这段代码:

  <?php
error_reporting(E_ALL);
if ($result == "00")
{

$url = 'http://127.0.0.1/xxxxxx/AccountCreator.ashx'; /*I've tried it a combination of ways just to see which might work */

$curl_post_data = array(
    'companyName' =>urlencode($companyName),
    'mainContact' =>urlencode($mainContact),
    'telephone1' =>urlencode($telephone1),
    'email' => urlencode($email),
    'contact2' => urlencode($contact2),
    'telephone2' => urlencode($telephone2)
    'email2' => urlencode($email2);
    'package' => urlencode($package)
    );

foreach($curl_post_data as $key=>$value) {$fields_string .=$key. '=' .$value.'&';
}
rtrim($fields_string, '&');
die("Test: ".$fields_string);

$ch = curl_init();

curl_setopt ($ch, CURLOPT, $url);
curl_setopt ($ch, CURLOPT_POST, count($curl_post_data));
curl_setopt ($ch, CURLOPT_POSTFIELDS, $fields_string);

$result = curl_exec($ch);

curl_close($ch);

之后,我的代码会发送一封电子邮件并执行一个 IF 语句。我知道这没问题,我只是在尝试插入此 cURL 请求时才开始遇到问题。

我试过了,但它没有运行。当我与支付合作伙伴整合时,它只是说:

Your transaction has been successful but there was a problem connecting back to the merchant's web site. Please contact the merchant and advise them that you received this error message. Thank you.

收到的确切错误是 HTTP 500 错误。

谢谢。

【问题讨论】:

  • 'email' => urlencode($email), 'contact2' => urlencode($contact2), 'telephone2' => urlencode($telephone2) 'email2' => urlencode($email2) ; 'package' => urlencode($package) 你真的应该修复这个数组!它缺少逗号,有一个分号..这一切都搞砸了
  • @sathia,谢谢,我修好了。不好意思错过了! die("测试:".$fields_string);现在正在输出:: Test: companyName=xxx&mainContact=xxx+xxx&telephone1=xxxx 以及所有正确的值。但这就是它输出的全部内容 - 它不会更新我的数据库,也不会继续执行以下 mail() 函数或 IF 语句。
  • 当然,现在你需要对 die() 进行注释,因为 die 执行 die 所说的:终止程序的执行。只需评论它 //die("Test.. 它会继续
  • 有道理!!我做到了,它很好地运行了我的脚本,甚至向我发送了一封确认电子邮件。虽然仍然没有更新我的数据库:-S 我假设这更多是 Web 服务的 URL 的问题,而不是我现在这个阶段的代码的问题。你怎么看?

标签: php curl http-post


【解决方案1】:
foreach($curl_post_data as $key=>value) {$fields_string .=$key. '=' .value.'&';

我猜这里的价值少了一美元

foreach($curl_post_data as $key => $value) {$fields_string .=$key. '=' .$value.'&';

您是否尝试过die($fields_string); 来查看您实际发送给商家的内容?

【讨论】:

  • 我尝试在它丢失的地方插入美元符号,但仍然没有乐趣。还是谢谢。
  • 你加了两个美元符号吗?什么死($fields_string);打印?
  • @sathia,是的,都是美元符号。我没有尝试 die($fields_string);打印件,我应该在哪里插入?
  • foreach($curl_post_data as $key=>$value) {$fields_string .=$key. '='.value.'&'; } rtrim($fields_string, '&');死($fields_string);
  • 嗨,sathia,我插入了那行,但是什么也没出现。我认为无论谁设置了服务器,都关闭了错误报告。有没有办法只为这个文件打开它?
【解决方案2】:

首先:您是在本地测试吗?因为您使用的 IP 不是有效的服务器地址。

设置 URL 的常量称为 CURLOPT_URL:

curl_setopt ($ch, CURLOPT_URL, $url);

CURLOPT_POST 也必须是真或假 (http://php.net/curl_setopt),而不是数字(可能是 1 除外):

curl_setopt ($ch, CURLOPT_POST, true);

这是一些 POST 示例代码:PHP + curl, HTTP POST sample code?

【讨论】:

  • @DanMan,感谢您的回复。不,我正在服务器上进行测试。我要连接的 Web 服务现在位于不同的端口上,它代表 IP 地址末尾的 :00。这是我不确定的事情之一——我是否可以使用端口号?
  • 我想它应该可以工作,但你也可以尝试设置 CURLOPT_PORT。
  • 如果你直接在服务器上测试,那么 IP 应该是 127.0.0.1
  • 嗯,还是没有运气。无论如何都会继续努力
  • 在您上次的编辑中,您仍在使用 CURLOPT 而不是 CURLOPT_URL。
【解决方案3】:

最好能提供你的 PHP 版本。

从 PHP 5 开始,一些方便的函数被捆绑在核心而不是单独的 PECL 库中。

// If you are working with normal HTTP requests, simply do this.
$curl_post_data = http_build_query($curl_post_data);

curl_setopt($ch, CURLOPT, $url);

// This is a boolean option, although passing non-zero integer 
// will be type-casted to TRUE, count() is not the proper way.
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);

// If you really want the next statement be meaningful, do this. 
// Otherwise your HTTP response will be passed directly into 
// the output buffer.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$result = curl_exec($ch);

请记住,PHP 中的 CURLOPT_POSTFIELDS 支持文件上传,方法是添加一个“@”字符,后跟一个完整的文件路径作为值。

您不想在这种情况下调用 http_build_query()。

文件上传示例代码

$curl_post_data = array('file1' => '@/home/user/files_to_be_uploaded');

虽然您可以选择指定 MIME 类型,但请参阅documentation 了解更多信息。

如前所述,首先检查您的 PHP 版本。此功能仅适用于 PHP 5,AFAIK 仍有公司在其服务器中托管 PHP 4.x。

【讨论】:

    【解决方案4】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 2011-04-22
      相关资源
      最近更新 更多