Jump to content

Recommended Posts

Hey Guys,

 

I'm just starting to learn Curl so that I connect to an API. At the moment I have this php code:

<?php

//******************************************************************************//
//																				//
//THIS FILE TAKES A CURRENT JOB AND ADDS IT TO A PAL LABEL PROJECT				//
//																				//
//******************************************************************************//


// ******* TURN ON ERROR REPORTING *****//
error_reporting(E_ALL);
ini_set('display_errors', '1');


//**** SET THE HEADERS UP FOR AUTHENTICATION *****//
$headers = array();
$headers[] = "X-SUBDOMAIN:XXXXXXXXXXX";  // Set the sub domain
$headers[] = "X-AUTH-TOKEN:XXXXXXXXXX";  // Set the API Code
$headers[] =  "application/json";

$total = 50;  // this is the max number of pages we will search. with 100 items on a page, and 100 items that means we can return 10,000 items
	
$curl = curl_init();
//**** WE GET ALL OF THE ITEMS CURRENTLY IN THE CURRENT PROJECT  AND PUT THEIR UNIQUE ID INTO THE ASSETS ARRAY ***//
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_URL, 'https://api.current-rms.com/api/v1/products?page=1&per_page=100');  // allows up to 1 million products
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);


// Send the request & save response to $resp
$resp = curl_exec($curl);
// Convert the response to an array

$obj = json_decode($resp, true);
// The Obj array currently contains every item in the DB and loads of useless info. We need to extract just the ID and the Name!

If I print_r the $obj variable it returns this:

 

Array
(
    [products] => Array
        (
            [0] => Array
                (
                    [id] => 85
                    [name] => 100m Lighting Multicore (2 x 5pin dmx & 2 x 3pin dmx & 1 x 16a )
                    [type] => Product
                    [tag_list] => Array
                        (
                            [0] => cable
                            [1] => lighting
                            [2] => multicore
                            [3] => checkweight
                        )

                    [description] => 
                    [allowed_stock_type] => 1
                    [allowed_stock_type_name] => Rental
                    [stock_method] => 1
                    [stock_method_name] => Bulk
                    [buffer_percent] => 0.0
                    [post_rent_unavailability] => 0
                    [replacement_charge] => 0.0
                    [weight] => 25.0
                    [barcode] => 
                    [active] => 1
                    [accessory_only] => 
                    [system] => 
                    [discountable] => 1
}

 

 

I have cut the output down a bit, each item in the products array is about 50 lines long, The only information I actually need to recall is the first two lines which is [id] and the [name]. Is there a simple way that I can do this? So far I have tried to do a Foreach loop and create a new array, but this take a very long time (there are about 600 records!) there must be a much simpler way, where I can unset everything that I don't need, or only call the information that I need from the API?
 

Here is the foreach loop that I used:

foreach ($obj as $key) {

	foreach ($key as $id => $value) {

		if (!empty($value['name'])) {
		$products[] = array('Name'=>$value['name'], 'ID' => $value['id'] );
		}
	}
}

**EDIT **

 

Once I have my array of ID's (which will be around 600 values long) I then need to get a list of serial numbers that are linked to these id's (there could be up to 100 per id) which will leave me with an array that will be huge! So far I have been getting gateway 504 error's, which I'm hoping is just due to the fact that I'm currently pulling out so much more information than I actually need, so I'm trying to make this more efficient and slim lined.  

 

I'm fairly new to PHP, and just do it as a hobby, so go easy on me please!

 

Thanks AJLX

Edited by AJLX
Link to comment
https://forums.phpfreaks.com/topic/303313-getting-started-with-curl/
Share on other sites

What's the point of the nested loop? Just iterate over the products:

<?php

$products = [];
foreach ($obj['products'] as $product)
{
    $products[] = ['id' => $product['id'], 'name' => $product['name']];
}

In any case, this will take some time, and your script must be able to handle that. Increase the execution limit and run this with a cron job, not via a browser request.

Thanks Jacques,

 

The nested loop was because I didn't know that I could do it your way. I'm not sure how I could run it with a cron job, I'm using it to suck the info out of an API, and store it in my own DB, so that I can then add my own data to it. The script only needs to run once, I'll then work out a way where I can make a refresh script which only imports the information that has changed.

 

Regards,

 

AJLX

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.