Hello I have this working CURL post request I wish to recode with guzzle. But I'm failing on a 400 Bad Request. Anyone see what I'm doing wrong:
$response = $this->getHttpClient()->post(
'https://login.eveonline.com/oauth/token', [
'headers' => [
'Authorization' => 'Basic '.base64_encode(
env('EVEONLINE_CLIENT_ID').':'.env('EVEONLINE_CLIENT_SECRET')
)
],
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token,
]);
return json_decode($response->getBody()->getContents(), true);
/*
$c = curl_init();
$cver = curl_version();
$contact = 'postmaster#asite.net';
curl_setopt($c, CURLOPT_URL, 'https://login.eveonline.com/oauth/token');
curl_setopt($c, CURLOPT_USERAGENT, "asite/admin (curl/{$cver['version']}; {$contact})");
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POSTFIELDS, http_build_query([
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token,
]));
curl_setopt($c, CURLOPT_HTTPHEADER, [ 'Authorization: Basic '.base64_encode(
env('EVEONLINE_CLIENT_ID').':'.env('EVEONLINE_CLIENT_SECRET')
) ]);
$rawjson = curl_exec($c);
return json_decode($rawjson, true);
*/
RESPONSE:
Client error: POST https://login.eveonline.com/oauth/token resulted in a 400 Bad Request response:
{"error":"invalid_request","error_description":"Unknown grant_type"}
Fixed it already sorry for the inconvenience:
$response = $this->getHttpClient()->post(
'https://login.eveonline.com/oauth/token', [
'headers' => [
'Authorization' => 'Basic '.base64_encode(
env('EVEONLINE_CLIENT_ID').':'.env('EVEONLINE_CLIENT_SECRET')
)
],
'json' => [
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token,
]
]);
Related
I have tried making a HTTP request using CURL as below:
$rawQuery = '{
"CUSTNAME" : "1970188",
"CURDATE":"2020-12-28T00:00:00+02:00",
"BOOKNUM":"Test BookNum",
"DETAILS":"Test Details"
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://somelink.co.de");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $rawQuery);
$curl_response = curl_exec($ch);
This one is returning me result. But when I try to implement the same using Symfony HTTP Client, I am getting 400 error.
This is the code, I have tried.
$response = $this->client->request('POST', $url, [
'auth_basic' => [
'username',
'password'
],
'headers' => [
'Accept' => 'application/json',
],
'json' => $rawQuery
]);
I am not sure what I am missing in Client
Can anybody please help me ?
Your Raw query is incorrect.. This should be a php array, Symfony is trying to json_encode() this for you when you use the 'json' key in the request. Reformat your $rawQuery to be
$rawQuery = [
"CUSTNAME" => "1970188",
"CURDATE" => "2020-12-28T00:00:00+02:00",
"BOOKNUM" => "Test BookNum",
"DETAILS" => "Test Details"
];
I want to extract data from this URL with Php Curl:
https://www.traveloka.com/en/flight/fullsearch?ap=JKTA.DPS&dt=14-10-2017.NA&ps=1.0.0&sc=ECONOMY
But I got the following error:
"{"errorType":"SERVER_ERROR","userErrorMessage":"Unexpected server error occurred. We apologize for the inconvenience. Please try again later.","errorMessage":"Exception occurred in server."}".
How I can extract the data from that link with php curl?
Here is my code
$url = 'https://api.traveloka.com/en/v2/flight/search/oneway';
$params = [
'context' => [
'tvLifetime' => 'eTq6r7InDN+j0vrg5Bujah9yFLWfBGsNGWxzjTBUa/jvVfn8fy/IF40U7OQl0vjmoqMJwuSocopqxISYLLi6YlngzuFViHSWhNHdFgs+49yydWXm5gSjBRwDBFuO0UKHd+B69Ip0Tk1qnKH+oyzW43f2GdS7QOd10yBpqoCOyOk73cVe4oyqCjYUR7X72PoHr14UQNQEUjl1NP5Mcxp+1Gw6RzKF7uV7jMRzmsYbGfGKpYLfsYtxaSx1t35KGWOO605YN9Mj2n5kP5fOD7j2KA9adtfLBtEymWXf6tEt3ug8oBVyzj5c2/pp/hboYilQnDRCih+RwhV5WX7hPTw9IsKapSNtWZ1NX8biH7UyYuhNLgcLK03OS4WNpoO+NphjOPKh09oBpUgrEJ0UqeY+1rfj98lWMAdpMO5rp2E5pvmP7HRuW6CqBwSchPLtVPQAi7ceDGYgYneH+AfodZMd5A==',
'tvSession' => '9tCFUug+5pqBk0WdAmwAbThaxD2lAm75JaxFJenJTB2MkEWW7bwVa5FW83NZnCLnlL2TAAijDDIDfD9YbC7NhRws3r5fKxPj62n1bJ+Nck309g3Rkogk+dtxsoMRpFHbkVkEJbYuNFbd9Ckp9iEBGg==',
'nonce' => '5eebdd23-2574-4465-afa7-cecc94b8f909'
],
'clientInterface' => 'desktop',
'data' => [
'currency' => 'IDR',
'destinationAirportOrArea' => 'DPS',
'flightDate' => [
'day' => '14',
'month' => '10',
'year' => '2017'
],
'isReschedule' => 'false',
'locale' => 'en_ID',
'newResult' => 'true',
'numSeats' => [
'numAdults' => '1',
'numChildren' => '0',
'numInfants' => '0'
],
'seatPublishedClass' => 'ECONOMY',
'seqNo' => 'null',
'sortFilter' => [
'filterAirlines' => [],
'filterArrive' => [],
'filterDepart' => [],
'filterTransit' => [],
'selectedDeparture' => '',
'sort' => 'null'
],
'sourceAirportOrArea' => 'JKTA',
'searchId' => 'null',
'usePromoFinder' => 'false',
'useDateFlow' => 'false'
],
'fields' => []
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_REFERER, "https://www.traveloka.com/en/flight/fullsearch?ap=JKTA.DPS&dt=14-10-2017.NA&ps=1.0.0&sc=ECONOMY");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Host: api.traveloka.com",
"Accept: application/json, text/javascript, */*; q=0.01",
"Accept-Language: en-us,en;q=0.5",
"X-Requested-With: XMLHttpRequest",
"Connection: keep-alive",
"Pragma: no-cache",
"Cache-Control: no-cache")
);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
Actually I'm not doing screen crawling, but I want to crawl its json data. I open the "Network" Tab in browser and see the XHR section, and then I want to grab the response from that XHR section. So how to do it and what's wrong with my code?
As per the URL you have given using the below code, you would get nothing rather the redirection link:
Replace with the link and parameters with the correct api:
General format of using CURL is given here:
$ch = curl_init();
$params = urldecode('{"ap":"' . 'JKTA.DPS' . '","dt":"' . '14-10-2017.NA' . '","ps":"' . '1.0.0'. '","sc":"' . 'ECONOMY'. '"}');
$url = 'https://www.traveloka.com/en/flight/fullsearch?JsonData=' . $params;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
if (!empty($output)) {
$x = json_decode($output, true);
var_dump($x);
}else{
return FALSE;
}
What you want to do is called Web scraping, there are plenty of tools that you can use to do that.
For now, go for:
https://github.com/FriendsOfPHP/Goutte
or https://github.com/duzun/hQuery.php.
I want to extract data from this URL with Php Curl:
https://www.traveloka.com/en/flight/fullsearch?ap=JKTA.DPS&dt=14-10-2017.NA&ps=1.0.0&sc=ECONOMY
But I got the following error:
"{"errorType":"SERVER_ERROR","userErrorMessage":"Unexpected server error occurred. We apologize for the inconvenience. Please try again later.","errorMessage":"Exception occurred in server."}".
How I can extract the data from that link with php curl?
Here is my code
$url = 'https://api.traveloka.com/en/v2/flight/search/oneway';
$params = [
'context' => [
'tvLifetime' => 'eTq6r7InDN+j0vrg5Bujah9yFLWfBGsNGWxzjTBUa/jvVfn8fy/IF40U7OQl0vjmoqMJwuSocopqxISYLLi6YlngzuFViHSWhNHdFgs+49yydWXm5gSjBRwDBFuO0UKHd+B69Ip0Tk1qnKH+oyzW43f2GdS7QOd10yBpqoCOyOk73cVe4oyqCjYUR7X72PoHr14UQNQEUjl1NP5Mcxp+1Gw6RzKF7uV7jMRzmsYbGfGKpYLfsYtxaSx1t35KGWOO605YN9Mj2n5kP5fOD7j2KA9adtfLBtEymWXf6tEt3ug8oBVyzj5c2/pp/hboYilQnDRCih+RwhV5WX7hPTw9IsKapSNtWZ1NX8biH7UyYuhNLgcLK03OS4WNpoO+NphjOPKh09oBpUgrEJ0UqeY+1rfj98lWMAdpMO5rp2E5pvmP7HRuW6CqBwSchPLtVPQAi7ceDGYgYneH+AfodZMd5A==',
'tvSession' => '9tCFUug+5pqBk0WdAmwAbThaxD2lAm75JaxFJenJTB2MkEWW7bwVa5FW83NZnCLnlL2TAAijDDIDfD9YbC7NhRws3r5fKxPj62n1bJ+Nck309g3Rkogk+dtxsoMRpFHbkVkEJbYuNFbd9Ckp9iEBGg==',
'nonce' => '5eebdd23-2574-4465-afa7-cecc94b8f909'
],
'clientInterface' => 'desktop',
'data' => [
'currency' => 'IDR',
'destinationAirportOrArea' => 'DPS',
'flightDate' => [
'day' => '14',
'month' => '10',
'year' => '2017'
],
'isReschedule' => 'false',
'locale' => 'en_ID',
'newResult' => 'true',
'numSeats' => [
'numAdults' => '1',
'numChildren' => '0',
'numInfants' => '0'
],
'seatPublishedClass' => 'ECONOMY',
'seqNo' => 'null',
'sortFilter' => [
'filterAirlines' => [],
'filterArrive' => [],
'filterDepart' => [],
'filterTransit' => [],
'selectedDeparture' => '',
'sort' => 'null'
],
'sourceAirportOrArea' => 'JKTA',
'searchId' => 'null',
'usePromoFinder' => 'false',
'useDateFlow' => 'false'
],
'fields' => []
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_REFERER, "https://www.traveloka.com/en/flight/fullsearch?ap=JKTA.DPS&dt=14-10-2017.NA&ps=1.0.0&sc=ECONOMY");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Host: api.traveloka.com",
"Accept: application/json, text/javascript, */*; q=0.01",
"Accept-Language: en-us,en;q=0.5",
"X-Requested-With: XMLHttpRequest",
"Connection: keep-alive",
"Pragma: no-cache",
"Cache-Control: no-cache")
);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
Actually I'm not doing screen crawling, but I want to crawl its json data. I open the "Network" Tab in browser and see the XHR section, and then I want to grab the response from that XHR section. So how to do it and what's wrong with my code?
As per the URL you have given using the below code, you would get nothing rather the redirection link:
Replace with the link and parameters with the correct api:
General format of using CURL is given here:
$ch = curl_init();
$params = urldecode('{"ap":"' . 'JKTA.DPS' . '","dt":"' . '14-10-2017.NA' . '","ps":"' . '1.0.0'. '","sc":"' . 'ECONOMY'. '"}');
$url = 'https://www.traveloka.com/en/flight/fullsearch?JsonData=' . $params;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
if (!empty($output)) {
$x = json_decode($output, true);
var_dump($x);
}else{
return FALSE;
}
What you want to do is called Web scraping, there are plenty of tools that you can use to do that.
For now, go for:
https://github.com/FriendsOfPHP/Goutte
or https://github.com/duzun/hQuery.php.
I am using the following api:http://apidocs.mifinity.com/#/doc/2 but its not working??
My API key
$data = array(
'key' => "1234567777"
);
$url = 'https://demo.mifinitypay.com/';
$ch = curl_init($url);
$postString = http_build_query($data, '', '&');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
$request = new HttpRequest();
$request->setUrl('https://demo.mifinitypay.com/api/oauth/token');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'postman-token' => '6396dfb7-540b-0e7d-7333-5d0eeff4d606',
'cache-control' => 'no-cache',
'authorization' => 'Basic username:password encoded using Base64',
'x-api-version' => '1',
'accept' => 'application/json',
'content-type' => 'application/x-www-form-urlencoded'
));
$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array(
'grant_type' => 'client_credentials'
));
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
I was using cURL for posting data to an API but have decided to switch to Guzzle. Using cURL I would do this
$data =
"<Lead>
<Name>$newProject->projectName</Name>
<Description>$newProject->projectName</Description>
<EstimatedValue>$newProject->projectValue</EstimatedValue>
</Lead>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.someurl.com/lead.api/add?apiKey=12345&accountKey=12345");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/xml',
'Content-Length: ' . strlen($data)
));
$output = curl_exec($ch);
This is what I am currently attempting with Guzzle.
$data = "<Lead>
<Name>$campaign->campaignName</Name>
<Description>$campaign->campaignName</Description>
<EstimatedValue>$campaign->campaignValue</EstimatedValue>
</Lead>";
$client = new GuzzleHttp\Client();
$req = $client->request('POST', 'https://somurl', [
'body' => $data,
'headers' => [
'Content-Type' => 'text/xml',
'Content-Length' => strlen($data),
]
]);
$res = $client->send($req);
$output = $res->getBody()->getContents();
The first problem I am facing is that it is stating that arguement 3 for request needs to be an array, and I am passing it a string. Thats fine, but then how can I send my xml block? Additionally, I think I may have set the headers incorrectly?
I have gone through the documentation and see that parameter 3 needs to be an array, but I do not see how to post an XML string.
Any advice appreciated.
Thanks
You can create an array using the 'body' param:
$client->request('POST', 'http://whatever', ['body' => $data]);
Read more at: http://docs.guzzlephp.org/en/latest/quickstart.html?highlight=post#post-form-requests
To set headers, you can do something like:
$response = $client->request('POST', 'http://whatever', [
'body' => $data,
'headers' => [
'Content-Type' => 'text/xml',
'Content-Length' => strlen($data),
]
]);
$output = $response->getBody()->getContents();
Read more at: http://docs.guzzlephp.org/en/latest/request-options.html#headers