I have an Excel files approx. 1,29 MB and it has 11500 lines of data.
In my PHP code first i read whole files and put in arrays via PHPExcel/IOFactory library.
After the putting, in foreach loop i start to create $postvars for cUrl.
gonderi_referans=REF&alici=Sarah&alici_telefon=55544448885&alici_adres=Krg&alici_ulke=DE
In foreach loop the code will create 11500 times $postvars and make curl request.
$url = "https://$_SERVER[HTTP_HOST]/api572.php";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,30);
curl_setopt($ch,CURLOPT_TIMEOUT, 20);
$response =json_decode(curl_exec($ch), true);
This operation takes approx 5 minutes. I want to do it in a faster way.
How can i do it?
Code read excel files in a 10 seconds, curl request it takes lots of times.
Try to use some batch-processing to minimize the number of sent requests, totally basic POC:
excel-reader.php:
<?php
// Dummy data START
$dummyExcelRows = [];
for ($i = 0; $i <= 11500; $i++) {
$dummyExcelRows[] = "gonderi_referans=REF&alici=Sarah&alici_telefon=55544448885&alici_adres=Krg&alici_ulke=DE&id=$i";
}
// Dummy data END
// Serialize multiple URI into one string START
$data = [];
foreach ($dummyExcelRows as $postvars) {
$data[] = $postvars;
}
$dataToSend = ['payload' => json_encode($data)];
// Serialize multiple URI into one string END
$url = "http://$_SERVER[HTTP_HOST]/test/api572.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataToSend);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
// $response =json_decode(curl_exec($ch), true);
$response = curl_exec($ch);
var_dump($response);
api572.php:
<?php
$decoded = json_decode($_POST['payload']);
foreach ($decoded as $row) {
echo "Do something with $row <br/>";
echo '<pre>';
parse_str($row, $fakePost);
print_r($fakePost);
echo '</pre>';
}
Related
Not sure how to word my question, but this is it:
I have an XML with 1000 products that I need to upload. I can only upload 100 products at a time with PHP. How do I make one PHP upload 100 products at a time until all products are uploaded?
Currently, my solution is to have 10 PHP files and 10 XML files each with 100 products. I would prefer if I can do this with 1 XML and 1 PHP file.
The reason I cannot do more than 100 products is that the sever I am uploading to doesn't allow me to more than 100 per instance.
I am using simplexml_load_file to load the files in the PHP
This is my code so far:
<?php
$data = Array();
$xml=simplexml_load_file("products_1.xml") or die("Error: Cannot create object");
foreach ($xml->product as $pr) {
$data[] = Array
(
"id" => (int)$pr->identifier,
"name" => (string)$pr->name,
...
);
}
// username & password
#$data = array_slice($data, 1,10);
#$data = $data[20];
$username = '...';
$password = '...';
//create hash
$hash = base64_encode($username . ':' . $password);
$headers = array(
'Authorization: Basic ' . $hash
);
// send data with cUrl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://.../api/product/save');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('data'=>($data))));
$result = curl_exec($ch);
// decode response
$all=json_decode($result);
// print array response
echo '<hr>';
echo '<pre>';
print_r($all);
echo '</pre>';
//echo '<hr>';
?>
For the other PHP files, I just replace the products_1.xml with the next number
I'm trying to run multiple cURL requests from the Google Analytics API and wanted to see if there was another more efficient way of running the requests than having to manually build them out like below. I would need to eventually build of about 10-15 requests so looking to build something more useful in that case.
<?php
$ch1 = curl_init();
$ch2 = curl_init();
$ch3 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "https://www.googleapis.com/analytics/v3/data/parameters_go_here");
curl_setopt($ch2, CURLOPT_URL, "https://www.googleapis.com/analytics/v3/data/parameters_go_here");
curl_setopt($ch3, CURLOPT_URL, "https://www.googleapis.com/analytics/v3/data/parameters_go_here");
curl_exec($ch1);
curl_exec($ch2);
curl_exec($ch3);
?>
you can use multi-curl
$urls = array($url1, $url2, $url3);
$curl_arr = array();
$inits = curl_multi_init();
for($i = 0; $i < count($urls); $i++)
{
$url =$urls[$i];
$curl_arr[$i] = curl_init($url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($inits, $curl_arr[$i]);
}
do {
curl_multi_exec($inits, $running);
} while($running > 0);
for($i = 0; $i < count($urls); $i++)
{
$results[] = curl_multi_getcontent($curl_arr[$i]);
}
print_r($results);
or create function
function doCurl($url){
$ch = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url);
curl_exec($ch);
}
Considering you're passing in different parameters each time, you do need to make separate calls to the API. Having said that, you may benefit from utilising a function() where you structure the call, and pass the parameter in as a variable:
function getData($param = "") {
$core_url = "https://www.googleapis.com/analytics/v3/data/";
$target_url = $core_url . $param;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $target_url);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
This will 'prettify' your code, allowing you to simply pass through a one word call. The following shows how you can access properties of the returned information:
getData('stats')->item; /* Calls https://www.googleapis.com/analytics/v3/data/stats */
getData('info')->item; /* Calls https://www.googleapis.com/analytics/v3/data/info */
Note that this will still result in the same amount of data being requested from the API, though provides a much cleaner way in which to call the API each time you need to.
Hope this helps! :)
I can't work out why this URL is not being found by CURL. The CURL engine is simply taken to a 400 error page.
My code is very simple and works fantastically with non-dynamic URLs.
I am hoping it's something easy to spot, for example, a missing CURL option.
I have tried using $url = urlencode($url) but that didn't work either.
Here's the code:
$url = 'http://www.destinations-uk.com/accommodations.php?link=accommodations&country=england&category=Reviews&id=1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$r = curl_exec($ch);
$r = explode("\n", $r);
$keys = array();
if(!empty($r)) $keys[] = array_shift($r);
foreach($r as $line){
preg_match('/.+:\s/',$line,$match);
if($match) $keys[substr($match[0],0,-2)] = preg_replace('/.+:\s/','', $line);
}
print_r($keys);
Perhaps, this is something on the server-side done to prevent automated requests.
My question is simple, but for me, it is creating so many confusions in my mind. I want to know that can we input an array to curl function ?
please note that 'I AM NOT POSTING DATA' (FOR POSTING DATA , I KNOW, ARRAY IS USED)
To make my question more clear, let me tell you the code ..
function mycurl($url){
$ch = curl_init(); // create a new cURL resource
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch); // grab URL and pass it to the browser
//echo $data; //ncomment for debugging
curl_close($ch);
return $data;
}
and array i am going to use is
myArray
0 => string 'http://www.abc.com/a
1 => string 'http://www.abc.com/b
2 => string 'http://www.abc.com/c
3 => string 'http://www.abc.com/d
to use the array, I am using foreach loop the code is given below
foreach ($myArray as $temp){
$heading= mycurl($temp);
echo $heading;
}
the purpose of the code is
go to each url of array using curl function extract required
data from the url process the next element of array and extract
data and so on
Can anybody guide me that How can i use array elements in curl function?
How can i get my objective? If foreach loop is not the correct methodology here, then what should I do?
What your doing looks good, except, you shouldn't initialize the curl handler for every iteration, just initialize it once, then change the $url value for every iteration, would look something like:
function mycurl($ch, $url) {
curl_setopt($ch, CURLOPT_URL,$url);
return curl_exec($ch);
}
$ch = curl_init(); // create a new cURL resource
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
foreach ($urls as $url) {
$header = mycurl($ch, $url);
var_dump($header);
}
curl_close($ch);
You're doing it right. However you can use curl_multi_exec() (see examples) to launch all requests at once, classical curl can do only one request at the time.
Little more effective way to do your code would be:
$ch = curl_init(); // create a new cURL resource
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
foreach( $myArray as $url){
curl_setopt($ch, CURLOPT_URL,$url);
$data = curl_exec( $ch);
echo $data;
}
curl_close( $ch);
Or with correct object design:
class MyClass {
protected $ch = null;
public function __construct( ){
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_HEADER, 0);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
}
public function __destruct(){
curl_close( $this->ch);
}
public function getData( $url){
curl_setopt($this->ch, CURLOPT_URL,$url);
return curl_exec( $this->ch);
}
}
$extractor = new MyClass();
foreach( $myArray as $url){
$data = $extractor->getData( $url);
echo $data;
}
I am using curl, I am wondering how would I send post/submit data on my page to those websites? The web site has "host, time, port". My MYSQL database has a list of urls. I was thinking of curl_multi but I am not sure.
Please someone post examples. It has to be a fast method.
Basically feteches the url and post.
while($resultSet = mysql_fetch_array($SQL)){
$ch = curl_init($resultSet['url'] . $fullcurl);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
The PHP cURL reference says that the CURLOPT_POST option, set to true, makes it a POST request. CURLOPT_POSTFIELDS sets the fields that you will send in foo=bar&spam=eggs format (which one can build from an array with http_build_query).
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'foo=bar&spam=eggs');
Here is an example on how to do it with curl_multi. Although you should break it up so you only have a certain amount of URLs going out at once (i.e. 30). I added the follow location directive, which you usually want.
$mh = curl_multi_init();
$ch = array();
while($resultSet = mysql_fetch_array($SQL)){
$ch[$i] = curl_init($resultSet['url'] . $fullcurl);
curl_setopt($ch[$i], CURLOPT_TIMEOUT, 2);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch[$i], CURLOPT_FOLLOWLOCATION, true);
curl_multi_add_handle($mh, $ch[$i]);
}
$running = null;
do {
curl_multi_exec($mh,$running);
} while ($running > 0);
$num = count($ch);
for ($i=0; $i<$num; $i++ ) {
curl_multi_remove_handle($mh, $ch[$i]);
}
curl_multi_close($mh);
Give this a shot:
while ($resultSet = mysql_fetch_assoc($SQL)) {
$ch = curl_init($resultSet['url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fullcurl);
$response = curl_exec($ch);
curl_close();
}