cURL post不工作​​PHP [英] cURL post not working PHP

查看:168
本文介绍了cURL post不工作​​PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

有效的程式码: http://svgen.com/jupiter.php

这是我的代码:

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

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

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

    $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.

推荐答案

将您的帖子数据设为:

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

并更改:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

这篇关于cURL post不工作​​PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆