Get Request Working fine with Postman but not with PHP CURL - php

I have simple GET request on postman and it does working fine
https://www.instagram.com/p/CVhuRABqnAI/?__a=1&__d=dis
When Im using PHP CURL its did not respond me with Json data rather showing logo
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0",
CURLOPT_URL => "https://www.instagram.com/p/CVhuRABqnAI/?__a=1&__d=dis",
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);
$error = curl_error($curl);
curl_close($curl);
if($error) {
echo "cURL Error:" . $error;
} else {
echo $response;
}
Can some one else me with this?

Finally I might know what to do as I just faced this problem.
Usually, there is just some kind of difference in between postman and curl. Or there is "something" else, because my solution was just to not send any headers at all...

Related

Download Whatsapp Business Media Image API

I am Using Whatsapp Media Api to get Image send from user to business number using webhooks and i also get the Image ID and url and Image as well in Postman but when I use that curl request made from Postman it alwasy shows facebook error:
Sorry, something went wrong.
We're working on it and we'll get it fixed as soon as we can.
But its working fine in Postman what am i doing wrong?
<?php
$ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.16 (KHTML, like Gecko) \
Chrome/24.0.1304.0 Safari/537.16';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'Image_URL_from_Api',
CURLOPT_USERAGENT => $ua,
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_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer My_Token'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
OK, I've got the answer I needed to give the user_agent value which is in postman it is automatically putting CURLOPT_USERAGENT => 'PostmanRuntime/7.26.10' in the header but for curl, I've added CURLOPT_USERAGENT => 'curl/7.64.1', and it worked.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent

NGINX 502 bad gateway Error when running thru CURL

I am using the following code to run curl.But for one url, I am getting 502 Bad Gateway error.
<?php
//$proxy = '127.0.0.1:80';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => '<requesturl>',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_SSL_VERIFYHOST=>false,
//CURLOPT_PROXY=>$proxy,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Cookie: PHPSESSID=e03338f51c56ada6870d530080127581'
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
print_r($response);
print_r($err);
curl_close($curl);
?>
I have removed https and put http and checked but for that also not working.
My PHP version is 5.6.
Thanks,
Rekha
I have found the answer.I have added user agent and it worked fine.
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
For dynamically get user agent in php,
$userAgent = $_SERVER['HTTP_USER_AGENT'];
This will help someone.
Thanks,
Rekha

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%

GET curl request only works in postman but not in php

I'm trying to get html as response but it only works in postman but not in php. In php I get different response like "Something went wrong..."
What I'm missing ?
Here is code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.avnet.com/shop/SearchDisplay?searchTerm=LMK316BJ476ML-T&countryId=apac&deflangId=-1&storeId=715839038&catalogId=10001&langId=-1&sType=SimpleSearch&resultCatEntryType=2&searchSource=Q&searchType=100&avnSearchType=all",
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;
}
postman response
php response
Thanks in advance.
I played around the CURLOPT_HTTPHEADER and figured out the site is validating 2 headers which are User-Agent and Accept-Language. I sent it correctly and it worked. Please find the headers below
CURLOPT_HTTPHEADER => array(
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
"Accept-Language:en-US,en;q=0.5"
)
Enjoyed figuring out the issue.
I had the same problem. This might solve the problem:
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
Last night, I spent more than 3 hours to solve the similar problem and at the end, I noticed it was related with server configuration. My sysadmin solved the problem by updating configuration, as I understand, some server configuration doesn't let you send CURL request same server.
Check below ticket:
https://talk.plesk.com/threads/curl-dont-resolved-correctly-at-same-server.295173/
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Accept: application/json","User-Agent: WooCommerce API Client-PHP/3.1.0"]);
After spending lots of time, above Header works for me.
remove this
CURLOPT_ENCODING => "",

cannot resolve URL in 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.

Categories