cannot resolve URL in PHP - php

When I make a request with cURL gives error "cannot resolve host URL". Should I encode URL? After I encoded Google API URL, it still gives error.
https://developers.google.com/people/api/rest/v1/contactGroups/batchGet
GET https://people.googleapis.com/v1/contactGroups:batchGet
Where is the problem?
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://people.googleapis.com/v1/contactGroups:batchGet",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

I don't see anything wrong with the code, and I tested it and it worked fine.
I guess it's some issue with the DNS that can't resolve the host.

Related

API returns NULL response sometimes

I am making a API request in my web app which looks like this:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://******.com/jdconnectionpool/view?
requestAction=JOB_FILE_DETAILS&job_no=7476709",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Everything looks to be fine. However, it returns null sometimes for the same set of params on calling multiple times. I do not seem to find any pattern either. Any far fetched information from someone who has experienced this phenomenon would be really appreciated.

GET request to ups.com returns 403 one one particular host only

UPS has recently raised their security standards. As a result, on one host in particular, I get a 403 when attempting to do a GET to their rates API:
"http://www.ups.com/using/services/rave/qcostcgi.cgi?accept_UPS_license_agreement=yes&10_action=4&13_product=GNDRES&14_origCountry=US&15_origPostal=98584&19_destPostal=33773&22_destCountry=US&23_weight=2.375&47_rate_chart=Regular+Daily+Pickup&48_container=00&49_residential=1"
(I'm doing the GET in PHP using cURL.) I notice that this host is using an older cURL (7.19.7) and and older NSS (NSS/3.27.1), but I have other hosts that are using these versions where the GET will work.
What can I do to track down the issue?
This works fine:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://www.ups.com/using/services/rave/qcostcgi.cgi?accept_UPS_license_agreement=yes&10_action=4&13_product=GNDRES&14_origCountry=US&15_origPostal=98584&19_destPostal=33773&22_destCountry=US&23_weight=2.375&47_rate_chart=Regular+Daily+Pickup&48_container=00&49_residential=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Response:
UPSOnLine4%1DM%98584%US%33773%US%108%3%130.41%0.00%130.41% 8:00 A.M.%
4%1DA%98584%US%33773%US%108%3%98.23%0.00%98.23%10:30 A.M.%
4%1DP%98584%US%33773%US%138%3%89.08%0.00%89.08%End of Day%
4%2DA%98584%US%33773%US%208%3%45.79%0.00%45.79%End of Day%
4%3DS%98584%US%33773%US%308%3%36.05%0.00%36.05%End of Day%
4%GND%98584%US%33773%US%008%3%16.86%0.00%16.86%End of Day%

Royal Mail API Error: httpCode":"404", "httpMessage":"Not Found", "moreInformation":"No resources match requested URI

I have the following on a page:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.royalmail.net/***/summary",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"x-ibm-client-id: ***",
"x-ibm-client-secret: ***"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
I have everything authorised from Royal Mail end and when run in Royal Mail sandbox it provides the desired results but when run from a page on my server it returns:
{ "httpCode":"404", "httpMessage":"Not Found", "moreInformation":"No resources match requested URI" }
Could anyone point me in the right direction to what could be causing this issue.

PHP cURL with API Key and Secret (Postman)

I'm working with an API which is made by a classmate. I used Postman to generate the PHP cURL for the connection, with Authorization Basic. This works perfectly. Now, I want to get rid of the Authorization Basic and use my own API Key & Secret (password). Are there any good ways to do this?
Thanks in advance!
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "Some link here",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic //something here",
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$result = json_decode($response,true);
}

IP long string filter

Hey,
i am trying to only show 1 bit of the long string i get from the api i got fram postman, the only thing i need to show is the city. How do i need do this?
i'm trying to find a way with php but i have no clue what to do
a:14:{s:10:"regionName";s:10:"California";s:6:"status";s:7:"success";s:4:"city";s:13:"Mountain View";s:8:"timezone";s:19:"America/Los_Angeles";s:7:"country";s:13:"United States";s:11:"countryCode";s:2:"US";s:3:"zip";s:0:"";s:3:"lon";d:-122.08499908447266;s:3:"isp";s:6:"Google";s:2:"as";s:19:"AS15169 Google Inc.";s:5:"query";s:7:"8.8.8.8";s:6:"region";s:2:"CA";s:3:"lat";d:37.42290115356445;s:3:"org";s:6:"Google";}
(im using the ip of google just for this question)
so the length of the city name changes!
the site where i got it frm http://ip-api.com/php/8.8.8.8
and the code i am using:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip-api.com/php/8.8.8.8",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
That's a product of running a PHP variable in a PHP serialize you can reverse it with unserialize
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip-api.com/php/8.8.8.8",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$responseArray = unserialize($response); //You probably need some error trapping here
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $responseArray["country"];
}

Categories