dream04110 2016-11-25 15:12
浏览 62
已采纳

卷曲发布错误

I want to get the value when I post with php CURL to this link:"https://www.turkiye.gov.tr/btk-numara-tasima" but when i try to do this, i can not get any value the code below just show me the page of the link. What is the problem?

<?php

$curl=curl_init();
$data = "txtMsisdn=5441234567&token=%7B730FD6-BC236F-6AE440-B5E1CB-338E67-00EA4E-0C7F28-58EE4A-3FA9F9-EAA9A2%7D";
curl_setopt($curl, CURLOPT_URL, "https://www.turkiye.gov.tr/btk-numara-tasima");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
$content = curl_exec($curl);


if(curl_exec($curl) === false){
    echo 'Curl error: ' . curl_error($ch);
}else{
    echo 'Opatation comlated without error';
    echo $content;
}

?>
  • 写回答

1条回答 默认 最新

  • dongtiao2066 2016-11-25 16:28
    关注

    You have two issues with your code:

    1) as you navigate a site with a real browser cookies are stored/updated when pages are get and sent back to the server on every subsequent request. You're not storing cookies nor sending them.

    2) the token you're passing varies upon each request. It's a kind a "secret code" to validate the request.

    To solve this, first you have setup curl options to manage cookies. Cookies will be stored in a small text file of your choice.

    Then you have to split the task in two steps:

    first step: load the page and parse the token

    second step: send the post request passing the parsed token

    This is some modification of your original code. You can run it from the command line too.

    The only non-trivial part is extracting the token but, actually, it's just string manipulation.

    <?php
    
    // Load the page
    
    $curl=curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://www.turkiye.gov.tr/btk-numara-tasima");
    // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_VERBOSE, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookies.txt' ); // <-- read/write cookies into a text file
    curl_setopt($curl, CURLOPT_COOKIEJAR,  'cookies.txt' ); // <-- read/write cookies into a text file
    $content = curl_exec($curl);
    curl_close( $curl );
    
    // Parse output to extract token
    
    $content = substr( $content, strpos( $content, '<input type="hidden" name="token" value="' ) + strlen( '<input type="hidden" name="token" value="' ) );
    $token = substr( $content, 0, strpos( $content, '"' ) );
    $token = urlencode( $token );
    
    // This is the number to query
    
    $number = '5441234567';
    
    // Put together token and number
    
    $data = 'txtMsisdn=' . $number . '&token=' . $token;
    
    // Perform post request
    
    $curl=curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://www.turkiye.gov.tr/btk-numara-tasima?submit");
    //curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_VERBOSE, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookies.txt' ); // <-- read/write cookies into a text file
    curl_setopt($curl, CURLOPT_COOKIEJAR,  'cookies.txt' ); // <-- read/write cookies into a text file
    $content = curl_exec($curl);
    curl_close( $curl );
    
    // Parse output
    
    $content = substr( $content, strpos( $content, '<div class="reminderContainer">' ) + strlen( '<div class="reminderContainer">' ) );
    $result = substr( $content, 0, strpos( $content, '</div>' ) );
    
    echo $result . "
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大