How to extract data within a chain [closed]


I need to extract the ID of products from Amazon . I am developing a small application to calculate the cost of buying and shipping products from online stores. He had managed to bring the information with cURL and XPath . With Amazon it did not work for me, so I found a library to use ItemLookUp (API AMAZON) with the ID of the product. As the way the application will work is by pasting the product link, I need some way to extract that information from the link (ID).


The data I need to extract is what is inside /dp/IDPRODUCTO/ ->B07281WC5W . Is there any function in PHP that allows me to extract that data? Already with that data I would complete my last puzzle piece.

I work the options that have given me but I've noticed that the Amazon link has 3 variations, so I'll have to make my changes, when I've adapted it to the application I'll tell you how to stay at the end.

Thank you.

Well, it worked for me and I adapted it to what I was developing, the variation was that I saw some products that did not have the same format in the link and then did not return any data.

require_once( "AmazonAPI.php");

$enlace = ($_POST["cotizar"]);


$listado = explode("/", $enlace);
$sku = $listado[5];

$items = $amazonAPI->ItemLookUp( $sku );

foreach ($items as $item) {

            return $item ;


More or less like that. On a cell phone you have a / more so I put a conditional to detect mobile and $ sku = $ listing [6];

Thanks for the help.

asked by Edy Lasso 09.06.2017 в 23:47

3 answers


You can use the explode () function from PHP :


 * URL de Amazon
$url = "";

 *  Tenemos 5 (slash) antes del identificador del producto
$listado = explode("/", $url);

 * Por lo tanto nuestro identificador será el elemento número 5
print $listado[5] . "\n";





How it works?

The explode () function of php cuts a string according to an element such as delimiter .

As explain PHP :


Returns an array of string, each one being a substring of the string parameter formed by the division made by the delimiters indicated in the delimiter parameter.

In that case, we start by defining our delimiter (in this case it will be the / ) and we count the amount of delimiters that exist to where the data is that we want to obtain (identifier):


link // / TotalMount-PlayStation-Slim-Mounts-wall-near / dp / B07281WC5W / ref = sr_1_2? s = videogames & ie = UTF8 & qid = 1497043401 & sr = 1-2-spons & keywords = playstation & psc = 1

What will tell us that the product identifier will be found in the fifth position of the array returned by explode ().

answered by 10.06.2017 / 00:18

If the URL will always have that structure (the ID is found after /dp/ ) you can use a regular expression to extract it:

$url = '';
$id = preg_replace('/^.*?\/dp\/([^\/]+).*$/', '$1', $url);
echo $id;

Here I leave you a functional example in Ideone.

answered by 10.06.2017 в 00:32

Depending on what you need, this function that I put together can help you. I tried it and it worked for the URL that you indicate. Try it and I hope it works for you:

function getCode ($url) {
    $copiar = false;
    foreach (explode ("/", $url) as $codigo) {
        if ($copiar) {
            return $codigo;
        if ($codigo == "dp") {
            $copiar = true;
    return "no encontrado";
echo getCode("https://");

The function used is explode , but for your case of URL dynamics I only found one pattern: /dp/ . After dp comes the code you need. The function only goes through the array that is explode and after finding dp the next item in the array returns.

answered by 10.06.2017 в 00:19