cURL 帖子不起作用 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15151739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:40:21  来源:igfitidea点击:

cURL post not working PHP

phppostcurl

提问by Maurício Giordano

I'm having trouble using cURL for a specific page.

我在为特定页面使用 cURL 时遇到问题。

A live code working: http://svgen.com/jupiter.php

实时代码工作:http: //svgen.com/jupiter.php

Here is my code:

这是我的代码:

    $url = 'https://uspdigital.usp.br/jupiterweb/autenticar';

    $data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "FileHere");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "FileHere");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
    curl_exec($ch);
    curl_close($ch);

Although I had used the same url and post data, file_get_contents worked:

虽然我使用了相同的 url 和发布数据,file_get_contents 工作:

    $options = array('http' => array('method'  => 'POST','content' => http_build_query($data)));
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    var_dump($result); 

Someone could help me? Thanks.

有人可以帮助我吗?谢谢。

回答by Hanky Panky

Most probably it is the SSL verification problem.

很可能是 SSL 验证问题。

Add

添加

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

Also if you use CURLOPT_PROTOCOLSoption, it should be HTTPSsince you are posting to a secure url

此外,如果您使用CURLOPT_PROTOCOLS选项,它应该是HTTPS,因为您要发布到安全网址

curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);    // you currently have http

回答by Sudhir Bastakoti

make your post data as:

使您的帖子数据为:

$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
$postData = "";
foreach( $data as $key => $val ) {
   $postData .=$key."=".$val."&";
}
$postData = rtrim($postData, "&");

and change:

并改变:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

to

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

回答by Zeeshan

$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');

should have been

本来应该

$data = http_build_query(array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1'));

It automatically url encodes your query string as well and is safer than manual methods..

它也会自动对您的查询字符串进行 url 编码,并且比手动方法更安全。

回答by Emre

Try these:

试试这些:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAINFO, '/etc/pki/tls/cert.pem'); // path is valid for RHEL/CentOS

This makes sure the resource you're "curling" has a valid SSL certificate. It is not recommended to set "CURLOPT_SSL_VERIFYPEER" to false (0).

这可确保您“卷曲”的资源具有有效的 SSL 证书。不建议将“CURLOPT_SSL_VERIFYPEER”设置为 false (0)。

回答by Alain Tiemblo

You're on a secure connection, why are you using :

您处于安全连接状态,为什么要使用:

curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);

Use instead :

改用:

curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);