I am trying to parse this url:
https://halykbank.kz/presscenter/novosti
The news are loaded by AJAX. In network I found URL, that must show loaded news, but there only
{"result":false,"hint":"NO_AUTHORIZATION_DATA"}
Why I am getting this? Here the URL, that I think must show me loaded news:
https://backend.halykbank.kz/struct/category-items?categoryId=199&sort=position%20desc&offset=200&limit=100
You must send these header
Auth-User-Id, Auth-Token, Auth-Time
composer require guzzlehttp/guzzle:~6.0
$client = new GuzzleHttp\Client();
$res = $client->get('https://api.github.com/user', ['auth' => ['user', 'pass']]);
echo $res->getStatusCode(); // 200
$json_string = $res->getBody();
$json_obj = json_decode($json_string); //return Object<BR>
$json_arr = json_decode($json_string,true); //return Array
Related
I am calling an API of a shipping company to get the POD (Proof of delivery) image of a delivery as per this document.
I am expecting to see an image but the the API response is null.
Here is my code:
$host = "api.shiplogic.com";
$accessKey = 'AKIA55D5DNTBI4X24BCM'; //Sandbox credentials
$secretKey = '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'; //Sandbox credentials
$requestUrl = 'https://api.shiplogic.com';
$uri = '/shipments/pod/images?';
$httpRequestMethod = 'GET';
$data = 'tracking_reference=';
$refnr = 'FQJNF'; //created for testing
require 'AWS/aws-autoloader.php';;
use Aws\Signature\SignatureV4;
use Aws\Credentials\Credentials;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Client\ClientInterface;
$signature = new SignatureV4('execute-api', 'af-south-1');
$credentials = new Credentials($accessKey, $secretKey);
$psr7Request = new Request($httpRequestMethod, $requestUrl.$uri.$data.$refnr);
$client = new Client([$requestUrl, 'timeout' => 30]);
$sr = $signature->signRequest($psr7Request, $credentials);
$response = $client->send($sr);
$json = $response->getBody()->getContents();
echo $json;
I have tried to var_dump() and print_r()
What am I missing or doing wrong?
[{"id":203913730,"parcel_id":0,"date":"2022-08-05T09:44:19.704Z","status":"delivered","source":"danieladmin","message":"POD file(s) captured","data":{"images":["https://shiplogic-backend-prod-infra-images-and-notes.s3.af-south-1.amazonaws.com/shipment-images/8155533-fe1e0e10-324c-4360-b2f4-be51f091f8bb.png?X-Amz-Algorithm=AWS4-HMAC-SHA256\u0026X-Amz-Credential=ASIA55D5DNTBLMDRXYD5%2F20220805%2Faf-south-1%2Fs3%2Faws4_request\u0026X-Amz-Date=20220805T113651Z\u0026X-Amz-Expires=86400\u0026X-Amz-Security-Token=IQoJb3JpZ2luX2VjEJ7%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCmFmLXNvdXRoLTEiRzBFAiB4vd1t%2F11LRUaoSCpqMdIP8gOuT9L32p1LSyCzSiGUBQIhAMt8FtNGK6ibRWvAJtIf%2FIGvOsESyCw2bIgNl27PWYBwKp0DCLv%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEQAhoMOTU1ODkyNTkxODEwIgwhD2CnEtahYg8Ck0Iq8QLMbochjBL6wQyuYPgOrOyvgliZs44WoRZqjDllfIPfa86R5TDw6hKI6aTaQLpNWUInFDnrGRu7sd%2BolgUsqnN17lr20S2h7Fy%2FnU0Rwv2z11AYSXgXrvxWbHVMJzngkyfteitp0GpD3cjK%2BdIJ9iKRS8g%2BO5WfFQKu99StSfkTWgJ%2Fo1myNwJoJkWkWhYiO1c%2BStpBSs0vdKhSKLOuNu3HBQIpWTQ1U8qnGvNdigAdz%2B7gCJwsaNqUH%2FHtl3xNSSbUSkEzqYLzdkjmKNFUC%2BrVAePsS4UnVhkMmWlnal%2BvSI%2FgY%2BDE1IuuhWYl7kuWa5SC6E5p2vngpN9lm0EnDSmK3OFsohMIJnu23WUXBJTxpmx%2Fb6KL%2FPrXapAhHccAz%2FJFmeU55%2FnMp0AqiuHjWYWE1ei1TPR2mhyj94wTW3Y4lUxhnvfeHz7QmPeh3KN3HAN0S2WHDf9Bv1gUD6bDshj1tiREoeChZzfp1ZbArO86AOgw%2BcSzlwY6nQFtpA37RYzkLVk52OW4g1tyja35Mfs6%2FykajH9IqkjuTLqvNmGIfrS7cLGgqhvdLUEs3QTIYDfPbjgkNsl5roEHbI7NO%2FhfrjIpBVmOsxJsqp62yL8Ze%2F29hgfug0TnycpXSg1bAOQ5ROelqQi5kcuy%2FTT3tePuZy1EO%2BXTm0tQbE3tf0XkLw34cQ6078ZAQJ7tyI4R4qRnWfdTkLtM\u0026X-Amz-SignedHeaders=host\u0026X-Amz-Signature=9a3d73a886012ac24e4719888156922d840a73d43569934a8de33ab2336f47b1"]}}]
<Error>
<Code>AuthorizationQueryParametersError</Code>
<Message>
X-Amz-Algorithm only supports "AWS4-HMAC-SHA256 and AWS4-ECDSA-P256-SHA256"
</Message>
<RequestId>ECGG4HGD86Y0PYAY</RequestId>
<HostId>
qZHFwL8gZ3GEibJ6UmgAoNd97EMVGe1Xw24RjYIKAqqrOi2Cx+YPmBJGoCy4opTiih5Nz5YlEuU=
</HostId>
</Error>
Your code is actually entirely correct as far as getting the response, but the problem is that the response you're trying to copy the URL from is still JSON encoded. This leaves some of the parts of the URL in an invalid, encoded state; specifically, the & is encoded to /u0026.
...?X-Amz-Algorithm=AWS4-HMAC-SHA256\u0026...
That /u0026 at the end of each parameter is the culprit. If you json_decode() the response, then you get the correct URL.
The URL I received when I ran the code and decoded the result seems to work. All I did was add a line and change the last line:
...
$json = $response->getBody()->getContents();
$result = json_decode($json, false);
echo $result[0]->data->images[0];
You could obviously do that without adding a line and just changing the line where $json is assigned, but I wanted to leave that line alone to make it easier to see exactly what was different.
...
$json = json_decode($response->getBody()->getContents(), false);
echo $json[0]->data->images[0];
I want to scrape a website and am using guzzle 7.4 and Symfony Dom Crawler
I successfully retrieved the HTML data But the website is using CDN to host some resources and they are not loading because the header is not sent to get those resources
below is code retrieving html
<?php
require "vendor/autoload.php";
use Symfony\Component\DomCrawler\Crawler;
// Url
$url = 'scrapingdomain.com';
$headers = [
'referer' => 'examplescrapingdomain.com'
];
$client = new \GuzzleHttp\Client([
'headers' => $headers
]);
// go get the data from url
$response = $client->request('GET', $url);
$html = ''.$response->getBody();
$crawler = new Crawler($html);
echo $html;
?>
If I access the CDN directly and set referer header I get a response of 200
Below Code
<?php
require "vendor/autoload.php";
use Symfony\Component\DomCrawler\Crawler;
// Url
$url = 'examplecdnresource.com/Images.png';
$headers = [
'referer' => 'examplescrapingdomain.com'
];
$client = new \GuzzleHttp\Client([
'headers' => $headers
]);
// go get the data from url
$response = $client->request('GET', $url);
$html = ''.$response->getBody();
$crawler = new Crawler($html);
echo $html;
?>
I want to get the scrapdomain.com get resources and download the cdn hosted images that it has
All I needed to do to get the CDN hosted content inside the scraped html is use file_get_content function and set referer stream to download the data no inside guzzle as i was getting css and image files
We are using the MTN PHP-SDK from the below URL
https://github.com/digitalmaterial/dep.api.auth.php
Code:
$path = '/subscription/' . $subscription_id;
$depClient = new MTNDEP\DEPClient($accessKey, $accessSecret, $apiKey, $baseUrl);
$requestBody = [];
$response = $depClient->createRequest(MTNDEP\DEPClient::DELETE, $path, null, $requestBody)->send();
$responseArray = json_decode((string) $response->getBody(), true);
Response:
Client error: `DELETE https://api.dep.mtn.co.za/subscription/` resulted in a `404 Not Found` response:
{"message":"No method found matching route subscription/ for http method DELETE."}
We are unable to find any MTN-DEP Cancel subscription PHP code. Please help us and let us know how can we delete the subscription from MTN end?
i have noticed 2 things :
1- in the mentioned documentation, the request sent like this
$depClient->createRequest('POST' '/subscription');
so i think you need to use this , please try to adhere with all steps
$depClient->createRequest('DELETE',$path); // Returns DEPClient object for chaining, see below
$depClient->getRequest(); // Will return the GuzzleHttp\Psr7\Request object with signed auth details for DEP API requests
$response = $depClient->send();
$statusCode = $response->getStatusCode(); // returns the http status code
$rawResponse = (string) $response->getBody(); // body, you will need to cast to string or echo to get the body data.
$responseArray = json_decode($rawResponse, true); // return json decode array.
2- make sure the $subscription_id is set and have a value before you send the request.
Stating to the documentation, you need to specify the customer ID and the subscription ID.
You have to make a DELETE request to the endpoint /customers/{id}/subscriptions/{subscriptionId}.
Maybe the path specified in your code should look like this:
$path = '/customers/' . $customer_id . '/subscriptions/' . $subscription_id;
$depClient = new MTNDEP\DEPClient($accessKey, $accessSecret, $apiKey, $baseUrl);
$requestBody = [];
$response = $depClient->createRequest(MTNDEP\DEPClient::DELETE, $path, null, $requestBody)->send();
$responseArray = json_decode((string) $response->getBody(), true);
I am getting the restful web service response as well. But I am not able to parse it properly
my code looks like this
include 'RestClient.class.php';
error_reporting(E_ALL);
// Client Profile
$url = "http://localhost/lgen/index.php/api/client";
$ex = RestClient::get($url,array('requestType' =>'viewClientProfile',
'username' => 'uname',
'pass' =>'pass'));
echo $response = $ex->getResponse();
$xml = simplexml_load_string($response);
when print $response I am getting data on browser but while trying to parse it am not getting any kind of data
echo $response = $ex->getResponse();
You are creating and echoing a variable at the same time.
Try this:
$xml = simplexml_load_string($ex->getResponse());
var_dump($xml);
I am very new to PHP, and need to use an HTTP Request to download a URL that is external to my server. That URL is a PHP function that returns JSON code which I have decode. Any suggestions?
I have tried basic code:
<?php
//Code for forming the url (which I'm sure is correct)
$url = ...
$response = fopen($url,"x+");
$response = json_decode($response);
echo $response;
?>
//javascript in a seperate file that calls the php code
var response = xmlhttp.responseText;
alert(response);
Try this:
<?php
$url = 'YOUR_URL_HERE';
$data = file_get_contents( $url ); // it is a JSON response as per your statement.
$data= json_decode($data);
print_r($data); // now, it's a normal array.
?>
You may use fopen if config allows, or cURL or fsockopen functions to do that
You could use:
$json_str = file_get_contents($url);
$json = json_decode($json_str, true);
Couldn't you use file_get_contents? E.g.
<?php
$url = "YOUR_URL";
$json = file_get_contents($url);
// handle the data
$data = json_decode($json, TRUE);
var_dump($data); // example
?>
If you are doing a lot of requests you can try using a http class like Buzz which will Help clean up your code https://github.com/kriswallsmith/Buzz