首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用php curl将数据发布到ASP站点

问如何使用php curl将数据发布到ASP站点
EN

Stack Overflow用户
提问于 2014-09-02 11:44:57
回答 1查看 2.1K关注 0票数 2

我需要张贴数据到ASP网站使用php卷曲从我的网站。

该网站为http://www.hotfrog.com.au/AddYourBusinessSingle.aspx

对于这一点,我如下所示

  1. 使用PHP curl从该网站抓取网页html源代码以维护cookie和会话。
  2. 从源asp隐藏变量值中提取。
  3. 准备好带有所需表单字段的post字符串
  4. 并将这些数据发布到使用PHP curl的ASP站点url中,但是响应是页面表单信息,没有条目详细信息,甚至没有显示来自curl响应的非条目字段的验证消息。
  5. 对于此过程,CURLOPT_USERAGENT、CURLOPT_COOKIEJAR、CURLOPT_COOKIEFILE保持相同的值。

ASP站点需要的表单字段如下所示

ctl00$contentSection$CompanyDetailsControl$txtBusinessName

ctl00$contentSection$CompanyDetailsControl$txtSuburb

是直接张贴这些字段名,还是在PHP curl发布到目标站点之前需要对这些字段名进行编码?

任何人都可以通过php curl为该解决方案或其他任何ASP站点提供解决方案吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-02 07:01:20

使用

代码语言:javascript
复制
function get_headers_from_curl_response($headerContent) {

    $headers = [];

    // Split the string on every "double" new line.
    $arrRequests = explode("\r\n\r\n", $headerContent);

    // Loop of response headers. The "count() -1" is to
    //avoid an empty row for the extra line break before the body of the esponse.
    for ($index = 0; $index < count($arrRequests) - 1; $index++) {

        foreach (explode("\r\n", $arrRequests[$index]) as $i => $line) {
            if ($i === 0) {
                $headers[$index]['http_code'] = $line;
            }
            else {
                list ($key, $value) = explode(': ', $line);
                $headers[$index][$key] = $value;
            }
        }
    }

    return $headers;
}

function regexExtract($text, $regex, $regs, $nthValue) {
    if (preg_match($regex, $text, $regs)) {
        $result = $regs[$nthValue];
    }
    else {
        $result = "";
    }

    return $result;
}

$regexViewstate = '/__VIEWSTATE\" value=\"(.*)\"/i';
$regexEventVal   = '/__EVENTVALIDATION\" value=\"(.*)\"/i';

$ch = curl_init("__YOUR__URL__HERE__");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');

$response = curl_exec($ch);
curl_close($ch);

$viewstate = regexExtract($response, $regexViewstate, $regs, 1);
$eventval  = regexExtract($response, $regexEventVal, $regs, 1);

$params = [
    '__EVENTTARGET'       => '',
    '__EVENTARGUMENT'     => '',
    '__VIEWSTATE'         => $viewstate,
    '__EVENTVALIDATION'   => $eventval,
    'ctl00%24txtUsername' => 'xxx',
    'ctl00%24txtPassword' => 'xxx',
    'ctl00$ImgLogin.x'    => '0',
    'ctl00$ImgLogin.y'    => '0',];

$ch2 = curl_init("http://gsc.klub-modul.dk/cms/ShowContentPage.aspx?ContentPageID=1");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_HEADER, 1);
curl_setopt($ch2, CURLOPT_POST, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch2, CURLOPT_COOKIE, 'cookies.txt');
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookies2.txt');

$response2 = curl_exec($ch2);
curl_close($ch2);

foreach (get_headers_from_curl_response($response2) as $value) {
    foreach ($value as $key => $value2) {
        echo $key.": ".$value2."<br />";
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25622636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档