简体   繁体   English

cURL帖子不工作PHP

[英]cURL post not working PHP

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. 谢谢。

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_PROTOCOLS option, it should be HTTPS since you are posting to a secure url 此外,如果您使用CURLOPT_PROTOCOLS选项,则它应该是HTTPS,因为您要发布到安全URL

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

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);
$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编码,并且比手动方法更安全。

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. 这可以确保您“卷曲”的资源具有有效的SSL证书。 It is not recommended to set "CURLOPT_SSL_VERIFYPEER" to false (0). 建议不要将“CURLOPT_SSL_VERIFYPEER”设置为false(0)。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM