M
M
maksam072017-04-20 22:32:57
Slider
maksam07, 2017-04-20 22:32:57

How to use CURL which is called by timer ( js ) multiple times?

Basically, I don't know how to describe it. I want to parse JSON data of one site. If you need to parse 1 request, then CURL parses perfectly, returns the result and everything is displayed correctly. But if there are at least 2 requests, that is, when you click on the
"Start" button (conditionally), the timer starts. The first time there is no result, that is, some kind of error, I could not parse the data, but the second time it parses normally and the result is returned. It's the same if there are 2-... a lot of requests. Let it be 2 - 10. ajax will return an error everywhere, but only the last request will return with the result. I thought that maybe curl does not have time to parse somehow. I set the timer to an interval of 5 seconds and still, it returned an empty result on all requests and only worked on the last one. Well, or if this is 1 request, then the result was also. And!!!with file_get_contents parses all 10 requests with a result. Access to CURL and file_get_contents is the same.
Right now I'll try to throw a little code:

<button type="button" id="startPars">Парсить</button>

var timerId;
  function PPars(){
    $.ajax({url: '/ajax/query.php', type: 'POST', data: $("form#one").serialize(), dataType: "JSON",
      error: function (){ $("#result").append( "<div>Внутренняя ошибка</div>" ); },
      success: function ( a ) { 
        if ( a.s ) { 
          if ( a.log )
            $("#result").append( a.log ); 
        } else { 
          $("#result").append( a.log );
        }
        if ( a.tmr )
          timerId = setTimeout( PPars, a.tmr );
      }
    });
  }

not a working version of the function
$getRes = curl( "сайт какой-то" );
  $getRes = json_decode($getRes, true);
  // обрабатываем результат...
  $data = array( "s" => $s, "log" => $log, "tmr" => $tmr );
  echo json_encode($data);

  function curl($url) {
    $ch = curl_init($url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
  }

working version of the function
function curl($url) {
    $ctx = stream_context_create(
      array('http'=>
        array(
          'timeout' => 1200,
        )
      )
    );
    $response = file_get_contents( $url, false, $ctx );
    return $response;
  }

part of the code, of course, is not posted, but I think I provided all the basic information. Why does the curl return the result with one request, and if there is a timer for 2 or more repetitions, then only the result is returned with the last request?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
ma3xak, 2018-05-05
@ma3xak

Look at the owl-carousel library, it might help

D
dummyman, 2017-04-20
@dummyman

CasperJS
PhantomJS
SlimerJS
Here is a banal example of parsing 10 results of two queries from Google

var links = [];
var casper = require('casper').create();

function getLinks() {
    var links = document.querySelectorAll('h3.r a');
    return Array.prototype.map.call(links, function(e) {
        return e.getAttribute('href');
    });
}

casper.start('http://google.fr/', function() {
   // Wait for the page to be loaded
   this.waitForSelector('form[action="/search"]');
});

casper.then(function() {
   // search for 'casperjs' from google form
   this.fill('form[action="/search"]', { q: 'casperjs' }, true);
});

casper.then(function() {
    // aggregate results for the 'casperjs' search
    links = this.evaluate(getLinks);
    // now search for 'phantomjs' by filling the form again
    this.fill('form[action="/search"]', { q: 'phantomjs' }, true);
});

casper.then(function() {
    // aggregate results for the 'phantomjs' search
    links = links.concat(this.evaluate(getLinks));
});

casper.run(function() {
    // echo results in some pretty fashion
    this.echo(links.length + ' links found:');
    this.echo(' - ' + links.join('\n - ')).exit();
});

Issues
$ casperjs googlelinks.js
20 links found:
 - https://github.com/casperjs/casperjs
 - https://github.com/casperjs/casperjs/issues/2
 - https://github.com/casperjs/casperjs/tree/master/samples
 - https://github.com/casperjs/casperjs/commits/master/
 - http://www.facebook.com/people/Casper-Js/100000337260665
 - http://www.facebook.com/public/Casper-Js
 - http://hashtags.org/tag/CasperJS/
 - http://www.zerotohundred.com/newforums/members/casper-js.html
 - http://www.yellowpages.com/casper-wy/j-s-enterprises
 - http://local.trib.com/casper+wy/j+s+chinese+restaurant.zq.html
 - http://www.phantomjs.org/
 - http://code.google.com/p/phantomjs/
 - http://code.google.com/p/phantomjs/wiki/QuickStart
 - http://svay.com/blog/index/post/2011/08/31/Paris-JS-10-%3A-Introduction-%C3%A0-PhantomJS
 - https://github.com/ariya/phantomjs
 - http://dailyjs.com/2011/01/28/phantoms/
 - http://css.dzone.com/articles/phantom-js-alternative
 - http://pilvee.com/blog/tag/phantom-js/
 - http://ariya.blogspot.com/2011/01/phantomjs-minimalistic-headless-webkit.html
 - http://www.readwriteweb.com/hack/2011/03/phantomjs-the-power-of-webkit.php

That is, the links at the top of the answer are scripted browsers.
No curl needed. You give commands
This can even be run on a server. That is, it can be used in production.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question