php curl post数组_PHP: curl_setopt_array - Manual

Once upon a time I've got an error like "Problem with the SSL CA cert (path? access rights?)". Since what I was doing was pretty much an administrative task with no actual security issues involved, I decided to disallow certificate validation and this is where the most interesting stuff began.

First I did it like this and it worked:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

Next I thought, "But hey, I don't want any hardcoded stuff here. Let's use it in a configurable way!". And so I did something like this:

// in configuration

$CURL_OPTIONS = array(CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_SSL_VERIFYHOST => 0);

...........

// in place of two direct calls from earlier

curl_setopt_array($ch, $CURL_OPTIONS);

And I was so happy, there was no error anymore... and do you think I was happy for a long time? If so, then you're wrong. It stopped giving an error, while it didn't start to work!

I checked the actual data but they were allright. Then I thought: "Is it the curl_setopt_array() problem? Let's make it a cycle." The way it is mentioned in this help, actually.

foreach ($CURL_OPTIONS as $name => $value)

{

curl_setopt($ch, $name, $value);

}

And... it did not work the same way as with the curl_setopt_array() call. And the data were still allright...

So, if by chance you can't set CURL options with the curl_setopt_array() call, then now you know what to do and you know it is definitely not you who is to blame.

P.S.

By the way, the configuration used was:

Linux i-ween.com 3.2.0-4-amd64 #1 SMP Debian 3.2.73-2+deb7u3 x86_64

PHP Version 5.5.17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP Laravel是一个流行的Web开发框架,可以简化PHP开发过程并提供更好的开发体验。curl_setopt函数是PHP中用于设置cURL选项的函数,而cURL是一个用于发送和接收HTTP请求的库。下面是使用PHP Laravel和curl_setopt函数进行POST请求的示例。 1. 在Laravel中,首先要确保cURL扩展已经安装和启用。可以在php.ini文件中将以下行取消注释: extension=curl 2. 在Laravel应用中,可以使用Guzzle HTTP客户端库作为curl_setopt函数的替代方案。Guzzle库提供了更简单而又强大的HTTP请求功能,可以使用Laravel的composer工具进行安装: composer require guzzlehttp/guzzle 3. 使用Guzzle库进行POST请求的示例代码如下: ```php use GuzzleHttp\Client; $url = 'http://example.com/api'; // POST请求的URL $data = ['name' => 'John', 'email' => '[email protected]']; // POST请求的数据 $client = new Client(); $response = $client->post($url, ['form_params' => $data]); $statusCode = $response->getStatusCode(); // 获取响应的HTTP状态码 $responseData = $response->getBody()->getContents(); // 获取响应的内容 // 处理响应数据 ``` 在上面的示例中,使用Guzzle库创建了一个HTTP客户端实例$client。然后使用post方法发送POST请求,传递了请求的URL和POST数据。响应对象$response提供了一些方法来获取响应的状态码和内容。 使用curl_setopt函数进行POST请求的示例代码如下: ```php $url = 'http://example.com/api'; // POST请求的URL $data = ['name' => 'John', 'email' => '[email protected]']; // POST请求的数据 $curl = curl_init($url); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); // 处理响应数据 ``` 在上面的示例中,通过curl_init函数初始化cURL会话,并设置请求的URL。然后使用curl_setopt函数设置POST请求,传递请求的数据和其他选项。使用curl_exec函数执行请求,并使用curl_getinfo函数获取响应的HTTP状态码。最后使用curl_close函数关闭cURL会话。 以上就是使用PHP Laravel和curl_setopt函数进行POST请求的示例代码。根据具体情况,可以选用Guzzle库或curl_setopt函数来发送POST请求,并根据响应进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值