PHP Curl and XML Response - Updated - php

so after some great help, I've managed to get the script doing everything I need it to, but I need help with the next bit.
Please see my script below.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://www.xxxxx.com/webAPI/api/stock',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_POSTFIELDS =>'<?xml version="1.0" encoding="utf-8" ?>
<traderapi>
<token>T1a5c3t9</token>
<variant>Clearbank ltd</variant>
<pon>X</pon>
</traderapi>',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer T1a5c3t9',
'Content-Type: application/xml'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response
?>
I get the
Picture of Chrome results
The results that are displayed as this "210-AZBVTBC"
However in the code show this (as per the picture) "210-AZBVTBC4"
You can see I do a simple echo $response. How do I parse the information so I can show and manipulate how I show the individual tags. For example
echo ''.$response->Sku.'';

the result is a string not a file.
try using simplexml_load_string instead of simplexml_load_file.
$Products = simplexml_load_string($response);

Related

Amazon SP API - createFeedDocument

The goal is to do a "POST_FLAT_FILE_PRICEANDQUANTITYONLY_UPDATE_DATA" so right now I'm trying to do a createFeedDocument which I believe is the 1st step.
There's few questions about this but none in PHP curl.
I spent hours....HOURS and still can't figure it out..
I get error:
"message": null,
"code": "InvalidInput"
I even tried removing "contentType" from the URL and put it in the header, but then I get error msg: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method."
Please....please is there anyone out there that knows what I need to change with code below?
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://sellingpartnerapi-na.amazon.com/feeds/2021-06-30/documents?contentType=text/tab-separated-values;charset=UTF-8',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'x-amz-access-token: the token<hidden>',
'X-Amz-Date: 20230125T073105Z',
'Authorization: AWS4-HMAC-SHA256 Credential=The_AccessKey/20230125/us-east-1/execute-api/aws4_request, SignedHeaders=accept;host;x-amz-access-token;x-amz-date, Signature=a6e8bdf306ea24438978a5fff961d08fc65d06c10f43c782af15a6a60<hidden>'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

convert curl xml response to json or array in php

Good afternoon friends,
I need your knowledge, I have a curl php function which responds perfectly, but it has been a real headache for me to pass those results to a JSON or ARRAY, I have tried several ways without success,
the response i get from curl XML is this
0<?xml version="1.0" encoding="UTF-8"?> <mensaje> <version>2019.11.04</version> <nca-respuesta> <codigo-Error>0</codigo-Error> <texto-Error>Transaccion Exitosa</texto-Error> <transaccion> <boleta>2414</boleta> <tipo>1</tipo> <monto>24990</monto> <vendedor>111111</vendedor> <rutcp>1111111</rutcp> <numero-transaccion>2414</numero-transaccion> <folio-DTE>11111111</folio-DTE> <tipo-documento>2</tipo-documento> <fecha>04/11/2022</fecha> <sucursal>37</sucursal> <terminal>105</terminal> <factura-normal>0014122272 </factura-normal> <factura-moto> </factura-moto> <rut-emisor>111111</rut-emisor> </transaccion> <fidelizacion> <rut-bonificacion>111111</rut-bonificacion> </fidelizacion> <regalo> </regalo> <articulos> <articulo> <item>1</item> <upc>2000388411434</upc> <precio>16990</precio> <cantidad-comprada>1</cantidad-comprada> <rmc>2</rmc> <servicios> </servicios> <descuento> <tipo>1</tipo> <monto>4495</monto> </descuento> <cantidad-maxima-devolver>1</cantidad-maxima-devolver> <monto-maximo-devolver>9000000</monto-maximo-devolver> <rut-anfitrion>111111111</rut-anfitrion> </articulo> <articulo> <item>2</item> <upc>2000369667645</upc> <precio>16990</precio> <cantidad-comprada>1</cantidad-comprada> <rmc>2</rmc> <servicios> </servicios> <descuento> <tipo>1</tipo> <monto>4495</monto> </descuento> <cantidad-maxima-devolver>1</cantidad-maxima-devolver> <monto-maximo-devolver>9000000</monto-maximo-devolver> <rut-anfitrion>16665688</rut-anfitrion> </articulo> </articulos> <fpagos> <fpago> <tipo>4</tipo> <nombre>Tarjeta de Credito Bancaria</nombre> <marca>MA</marca> <tipo-tarjeta>C</tipo-tarjeta> <rut>14122272</rut> <numero-tarjeta>0</numero-tarjeta> <tipo-cuota>1</tipo-cuota> <numero-cuotas>1</numero-cuotas> <vencimiento>1111</vencimiento> <autorizacion>790425</autorizacion> <codigo-unico>111111111111111111</codigo-unico> <monto>24990</monto> <monto-devuelto>0</monto-devuelto> </fpago> </fpagos> <fpromocion> <id-cliente>111111111111</id-cliente> <tipo-id-cliente>1</tipo-id-cliente> <email-transaccion>cpruebas#pruebas.com</email-transaccion> </fpromocion> </nca-respuesta> </mensaje>
The other thing that I have tried is to pass that result to text to delete the line of text "0 " with $str but I have not had any success either,
I don't know what else to try, I leave you part of the function
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => '',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>"<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/' xmlns:xsi='' xmlns:xsd='' xmlns:ns2='' xmlns:ser='http://service/'>
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ser:NCAtomaticaDTE>
<arg0>
<![CDATA[
<mensaje/>
<nca-requerimiento>
<programa>
<version>Version VER6207I 23/01/2012 09:30</version>
<timeout>11</timeout>
</programa>
<datos-originales>
<fecha>$fecha_compra_boleta_formateada</fecha>
<sucursal>$sucursal_boleta</sucursal>
<terminal>$numero_terminal_boleta</terminal>
<numero-boleta>$numero_folio_boleta</numero-boleta>
<folio></folio>
<rmc></rmc>
</datos-originales>
</nca-requerimiento>
</mensaje>
]]>
</arg0>
</ser:NCAtomaticaDTE>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>",
CURLOPT_HTTPHEADER => array(
'SOAPaction: kkkk',
'Content-Type: application/xml'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I tried to pass the result to json or array with no success, I also tried to pass the result to text

How to add attachments to a page using the REST API and PHP?

I'm trying to add an attachment to a page using the REST API and PHP, but I can't get it to work. I got it to work with Postman, no problem, but the PHP code it spits out doesn't seem to be working, and it doesn't even give out an error message.
This is the code I've been using:
<?php
$curl = curl_init();
$restApiUrl = 'http://localhost:8090/rest/api/content/{pageId}/child/attachment/';
$filePath = 'C:/Users/{user}/Desktop/example.png';
$auth = '{auth}';
curl_setopt_array($curl, array(
CURLOPT_URL => $restApiUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('file' => new CURLFILE($filePath)),
CURLOPT_HTTPHEADER => array(
'Content-Type: image/png;charset=UTF-8',
'X-Atlassian-Token: no-check',
'Authorization: Basic ' . $auth
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
The values inside curly brackets (like {auth}) are just placeholders.
Nothing seems to happen with this, no error message, no attachment, nothing... All previous requests I've created in Postman (like creating new pages) usually worked in PHP right away, but not file uploads. Does anyone know what I'm doing wrong here?
Se the content type to:
"Content-Type: multipart/form-data"

WordPress JSON API AUTH with PHP Curl

I am working with wordpress and using this plugin https://wordpress.org/plugins/json-api-auth/ for JSON API Auth and I am trying to return success with curl in PHP but whenever I run the code I am getting {"status":"error","error":"Invalid username and\/or password."}
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="abcd"&password="abcd"',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
But when I put this in the browser url https://example.com/api/auth/generate_auth_cookie/?username=abcd&password=abcd I am getting success
{"status":"ok","cookie":"xyz","cookie_name":"xyzxyz","user":{"id":1,"username":"abcd","nicename":"abcd","email":"abcd#abcd.com","url":"","registered":"2020-09-10 10:42:41","displayname":"abcd","firstname":"abcd","lastname":"abcd","nickname":"abcd","description":"","capabilities":{"administrator":true},"avatar":null}}
Can you help me what I am doing wrong with curl in PHP?
Original version of CURLOPT_URL that I have used in code
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="'.$this->input->post('email').'"&password="'.$this->input->post('password').'"',
Remove the " from the cURL url query string. The WordPress PHP handling your request would read your username as "abcd" instead of abcd as you are trying to achieve.
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="abcd"&password="abcd"'
Should be
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username=abcd&password=abcd'

Requesting a cURL GET Request in PHP outputs garbage

I'm trying to build a Connector to Dynamics 365 Business Central but I'm having problems getting the data. Please help me figure out why when I send a GET request using cURL and PHP, it produces the following output:
�S[O�0�+(��&��qB��i�t�F/���=�ا��Ďb�}N�"&�M�����������8�0% �;
Here is my code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.businesscentral.dynamics.com/{tenantID}/customers/?$filter=displayName%20eq%20'Shawn%20Test'",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: {Auth Code}"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
It seems like a character encoding issue, or maybe you are receiving binary data.
What is this endpoint supposed to return? Check the response headers, it might give you a clue.
This is an encoding problem, try to remove CURLOPT_ENCODING line from your request or try to choose different encoding from your browser

Categories