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 . "
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了