Jump to content

speckytwat2

Members
  • Posts

    16
  • Joined

  • Last visited

Everything posted by speckytwat2

  1. Thanks, looking at that though the only problem is the JSON data being pulled from the API is going to be different each time, so I can't see how it would work- how would I throw all the raw JSON data into the objectArray variable? I can put it into a PHP variable and in fact tried creating a JSON file from that, so it would have whatever the queried data was on that occasion: $fp = fopen('result.json', 'w'); fwrite($fp, $result); //Where $result is the data pulled through from the API But I don't think the example script can read a specially created JSON file?
  2. Thanks... unfortunately I tried that: $decoded = json_decode($result, true); and $decoded = json_decode($result); The 1st just outputted "Array" and the 2nd threw an error: "Fatal error: Uncaught Error: Cannot use object of type stdClass as array in..." then "Stack trace: #0 {main} thrown in..." I also tried this: $decoded = json_decode($result, JSON_PRETTY_PRINT); But that still outputs the same data, albeit with Array => instances added. Just need a way to get this somehow exported to a HTML table?
  3. Thanks, I already used json_encode above, should I be using it on $result as well? i.e $resultencoded = json_encode($result); I tried that but it just seems to add slashes in front of the quotation marks? Also tried this: <pre><?php echo json_encode($result JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); ?></pre> and <pre><?php echo json_encode($result JSON_PRETTY_PRINT); ?></pre> But it still gives an unfriendly output, putting lots of instances of \n in place?
  4. Hi, I have a connection set up to an API using a PHP script - the API sends back data in JSON format, and if I capture it and echo it, it displays in extremely unfriendly format on the screen. I've tried to find a way to convert this data into readable format, ideally in a HTML table, but although there are articles on converting manually inputted JSON data into a HTML table using Javascript, I can't find a way to do it from a PHP variable. This is what I have so far: if ($_POST['getcompany']) { $companyname = $_POST['_Name']; $ch = curl_init(); $data_array2 = array( 'token' => $token ); $make_call2 = json_encode($data_array2); //echo 'Token is '.$token; $token2 = substr($token, 14); $token3 = substr($token2, 0, -3); //echo 'Token 2 is '.$token3; //echo 'Company Name is '.$companyname; //curl_setopt($ch, CURLOPT_GET, 1); //curl_setopt($ch, CURLOPT_POSTFIELDS, $make_call2); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: '.$token3.'')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, 'https://connectionurl?countries=GB&name='.$companyname); //Execute the request $result = curl_exec($ch); echo 'Companies: '.$result; //This displays the data provided by the search, but is in JSON format So I need a way to get the data held in $result, into a HTML table. Any idea how I can do this?
  5. UPDATE - I did some digging around and saw a tiny dropdown arrow under Authenticate in their documentation which reveals a different connection URL- so it looks as if the URL they gave me wasn't correct and it needed to have "authenticate" after it. Anyway it now comes back with an Access Denied message which is obviously a separate issue so I will see if I can get that reset and try again. Thanks for your assistance so far.
  6. I'm authenticating by sending the username and password as follows which I believe is what Creditsafe are asking people to do? $data_array = array( 'username' => "email@example.com", 'password' => "passwordstring" ); $make_call = json_encode($data_array); And then I send it using: curl_setopt($ch, CURLOPT_POSTFIELDS, $make_call); If that isn't correct, how would I send the username and password? Thanks. (I did also try adding the following to authenticate, but that didn't send back anything meaningful either) curl_setopt($ch, CURLOPT_USERPWD, "email@example.com:passwordstring");
  7. Thanks, well I tried the following based on your suggestions: $ch = curl_init(); $data_array = array( 'username' => "email@example.com", 'password' => "passwordstring" ); $make_call = json_encode($data_array); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $make_call); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, "https://connect.sandbox.creditsafe.com/v1"); //Execute the request $result = curl_exec($ch); echo 'Result: '.$result; $response = json_decode($result, true); echo 'Response: '.$response; if(curl_errno($ch)){ echo 'Request Error:' . curl_error($ch); } echo 'Array is '.var_dump($response); curl_close($ch); [/CODE] So the results are: First the raw result: Result: { "correlationId": "8222f970-59cd-11eb-893a-0223445bacd9", "message": "Resource not found" } The json-decoded Response: Response: Array And if I do a var_dump: array(2) { ["correlationId"]=> string(36) "8222f970-59cd-11eb-893a-0223445bacd9" ["message"]=> string(18) "Resource not found" } So it seems to be getting something from somewhere but isn't able to find the token or authenticate. The URL is definitely correct, tech department confirmed. User / pw are ones that they set up. I did also add the following to capture any errors but apparently there weren't any: if(curl_errno($ch)){ echo 'Request Error:' . curl_error($ch); }
  8. Just to add, I also tried including this in the CURL array: CURLOPT_POSTFIELDS => $make_call, $make_call being the JSON encoded array of username and password declared earlier: $data_array = array( "username" => $username, "password" => $password ); $make_call = json_encode($data_array);
  9. In the Authentication section near the beginning, here: https://www.creditsafe.com/gb/en/enterprise/integrations/api-documentation.html It shows the sample JSON code for authentication on the right, but doesn't indicate exactly how I would use it or send the request from within a PHP system which is what I'm trying to do. So I put together a script using CURL (the one I posted above) to send what I hoped would be the relevant data, but obviously that isn't working- and they refuse to provide anything more than what's in the documentation.
  10. Unfortunately their documentation doesn't have much that's specific, it is simply "Do this" with some minimal examples, which is why I had to try and patch something together and do a lot of guesswork. Their support people have said they're not going to help so I'm kinda stuck.
  11. I've read the documentation about using CURL to send JSON information to an API, but from what I've read there are various ways of doing this... I've tried various suggested methods- but I haven't been able to get it to work... hence my question.
  12. Thanks. At first I thought it might be ok, although the token string is supposed to be longer than that. Then, I manually changed the password so it should no longer work at all... and it still outputted that string (or one of the same length)- so that appears to be coming from somewhere or something else... but I have absolutely no idea what it is. ??? I also wondered if I needed to use json_encode when giving the username and pw to the API (I put a line for that in the code although I didn't end up using it). That said, I wasn't sure how to use CURL to send that JSON data through...
  13. Hi, I'm trying to connect to CreditSafe's API with supplied username and password using PHP's CURL, unfortunately am having to do everything from scratch as they won't provide any details as to how we are supposed to set the connection up. The idea is that a username and pw are supplied and you then get a "token" which can be used to make further requests for data. The username and pw are given by them in JSON format so I am trying to use these and authenticate against their API. At the moment though I get the following for each of the test variables (see end of this script) I'm outputting to the screen: Data is HTTP/1.1 100 Continue HTTP/1.1 404 Not Found Date: Mon, 18 Jan 2021 15:18:30 GMT Content-Type: application/json; charset=UTF-8; skipnullon="everywhere" Content-Length: 61 Connection: keep-alive Strict-Transport-Security: max-age=31536000; includeSubdomains; { "correlationId": "6c2dc370-59a0-11eb-893a-0223445bacd9" } Response is NULL Array is just blank (because Response is NULL) This is what I have so far, am struggling with it, any idea what's going wrong? <?php $curl = curl_init(); $username = "email@domain.com"; $password = "passwordstring"; $data_array = array( "username" => $username, "password" => $password ); $make_call = json_encode($data_array); $headers = array( 'Content-Type:application/json', 'Authorization: Basic' ); curl_setopt_array($curl, [ CURLOPT_POST => 1, CURLOPT_URL => "https://connect.sandbox.creditsafe.com/v1", CURLOPT_HTTPHEADER => $headers, CURLOPT_HEADER => 1, CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => true, CURLOPT_MAXREDIRS => 10, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_USERPWD => $username . ":" . $password ]); $return = curl_exec($curl); $response = json_decode($return, true); //Testing output below echo 'Data is '.$return; echo 'Response is '.$response; echo 'Array is '.var_dump($response); curl_close($curl); ?> [/CODE] Anyone have any ideas? Thanks! ?>
  14. Thanks, is there a way to do the same thing using mysqli? I will look into PDO but need to fix this first- then I will go and learn PDO as soon as I can.
  15. Hi, I'm having issues trying to add up the totals of each product type in a while loop- I'm sure it's straightforward to fix but for some reason I can't find a way to get it to add up all the totals of the various product types and list them so we have say 5 of product 1, 11 of product 2, 8 of product 3 and so on. So firstly the user searches between two different dates / times to get the orders made during that time period: (I'm using separare date and time fields as the datetime-local input doesn't work in a lot of browsers) if($_POST['search']) { $searchdate1 = mysqli_real_escape_string($conn, $_POST['_Date1']); $searchdate2 = mysqli_real_escape_string($conn, $_POST['_Date2']); $searchtime1 = mysqli_real_escape_string($conn, $_POST['_Time1']); $searchtime2 = mysqli_real_escape_string($conn, $_POST['_Time2']); $datetime1 = $searchdate1 . ' ' . $searchtime1; $datetime2 = $searchdate2 . ' ' . $searchtime2; $nicedate1 = date("l F j, Y", strtotime($searchdate1) ); //Just used for readability in the report $nicedate2 = date("l F j, Y", strtotime($searchdate2) ); //Now get the orders from the db $getorders = $oc_conn->query("SELECT * FROM oc_order WHERE date_added >= '$datetime1' AND date_added <= '$datetime2' AND order_status_id != 0 ORDER BY order_id DESC"); //Then generate start of a table in HTML, the while loop that follows will populate the rows while ($row = $getorders->fetch_assoc()) { //Get the order data $firstname = $row["firstname"]; $lastname = $row["lastname"]; $fullname = $firstname . ' ' . $lastname; $orderid = $row["order_id"]; $orderdate = $row["date_added"]; $email = $row["email"]; $total = $row['total']; $total = number_format($total, 2); $niceorderdate = date('F j, Y H:i', strtotime($orderdate)); echo '<tr><td>'.$fullname.'</td><td>'. $orderid . '</td><td>'; $getproducts = $oc_conn->query('SELECT * FROM oc_order_product WHERE order_id = '.$orderid); //Product names and details are held in a separate table while ($row = $getproducts->fetch_assoc()) { $orderproductid = $row["order_product_id"]; $productname = $row["name"]; $quantity = $row["quantity"]; echo $quantity . ' x '.$productname.' <br>'; //This works for each individual order, but what I need is the total $quantity of each $productname, across ALL orders in the time range } echo '</td><td>'. $niceorderdate.'</td></tr>'; } echo '</tbody></table>'; } ?> [/PHP] As mentioned in the comment above, what I need is the total $quantity of each $productname, across ALL orders in the time range, so that I can see how many of each product ($productname) were ordered in total. I tried using SUM for quantity in the mySQL, and COUNT for adding up the different product names but for some reason it didn't work. Can someone let me know what I need to do? Thanks :)
×
×
  • 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.