cURL cannot connect to host - php

I am getting the issue in curl function. When I run the url direct in browser then it shows the content but when I use that url in curl function then it is showing the following message.
Array
7
couldn't connect to host
bool(false)
I am using the following code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$queryurl);
curl_setopt ($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'Host: http://cloud.softpanda.com.au'
));

The URL you specified returns a 301 redirect to http://www.softpanda.com.au, so you will likely not get the content unless you either correct the URL or set CURLOPT_FOLLOWLOCATION to TRUE, which will follow redirects such as what you're getting.

Related

Accessing the Aleph API with PHP, always getting error "No schema is specified for the query."

I'm trying to access the OCCRP Aleph API
https://redocly.github.io/redoc/?url=https://aleph.occrp.org/api/openapi.json
with the following PHP program
$url = 'https://aleph.occrp.org/api/2/entities?q="title:bank"';
$ch = curl_init();
$headeroptions = array('Accept: application/json', 'Content-Type: application/json', 'Authorization: ApiKey 363af1e2b03b41c6b3adc604956e2f66');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headeroptions);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$array = json_decode($data, true);
Var_dump($array);
I always get the error "No schema is specified for the query."
When I call Statistics, I get a correct answer...
after inspecting how the API works in their own website, I've notice you're missing a required parameter, which indicates the schema. Just add this to your url:
&filter:schemata=Thing
and it should work

how to convert curl command into php script

I have a situation where i want to use curl functionality to get data by api call.
the curl command i'm interested to convert look like this:
"curl -X POST \\ \n'http://test.payumoney.com/payment/op/getPaymentResponse?merchantKey=40747T&merchantTransactionIds=396132-58876806' \\ \n -H 'authorization: KpNTiy57L6OFjS2D3TqPod8+6nfGmRVwVMi5t9jR4NU= \\ \n -H 'cache-control: no-cache' \\ \""
I have tried this much
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://test.payumoney.com/payment/op/getPaymentResponse');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$array('merchantKey'=>40747,'merchantTransactionIds'=>396132-58876806))
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
please help me to correct above code!
thank in advance
You are missing a semicolon in your code and you don't need to declare the array as a variable.
curl_setopt($ch,
CURLOPT_POSTFIELDS,
array('merchantKey'=>40747, 'merchantTransactionIds'=>396132-58876806));
PHP Code
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://test.payumoney.com/payment/op/getPaymentResponse');
curl_setopt($ch, CURLOPT_POST, true);
// Add authorization header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'authorization: KpNTiy57L6OFjS2D3TqPod8+6nfGmRVwVMi5t9jR4NU=',
));
// Remove $ from array()
curl_setopt($ch, CURLOPT_POSTFIELDS, array('merchantKey'=>40747, 'merchantTransactionIds'=>396132-58876806))
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// For No-Cache
curl_setopt($curl1, CURLOPT_FRESH_CONNECT, TRUE);
$result = curl_exec($ch);
?>
You should do it like this, here i am assuming all your parameters are correct. You were missing some required parameter which were sending in curl command but not in PHP curl that is CURLOPT_HTTPHEADER and CURLOPT_HEADER and you should use http_build_query while sending array in CURLOPT_POSTFIELDS and You should use CURLOPT_FOLLOWLOCATION because your current is URL is redirecting from HTTP to HTTPS
<?php
ini_set('display_errors', 1);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "http://test.payumoney.com/payment/op/getPaymentResponse?merchantKey=40747T&merchantTransactionIds=396132-58876806");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('merchantKey'=>"40747",'merchantTransactionIds'=>"396132-58876806")));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('cache-control: no-cache',
'authorization: KpNTiy57L6OFjS2D3TqPod8+6nfGmRVwVMi5t9jR4NU='));
curl_exec($ch);

Getting Data via a PHP Curl Call

I am trying to make a PHP Curl Call. The call works in POSTMAN and looks like :
URL Endpoint: https://api.sb.example.com/v1/resource/identifier
Headers:
Content Type: application/json
Authorization : Bearer AccessTokenValue
HTTP Protocol : GET
This call returns me a valid JSON response. Now when i do the same using PHP, i am unable to get the response. Here is my PHP Curl call:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sb.example.com/v1/resource/identifier");
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type : application/json',
'Authorization : Bearer AccessTokenValue'
));
$data = curl_exec($ch);
print_r($data);
curl_close($ch);
This call via PHP gives me the error:
HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Any ideas/suggestions for the code ?
Please Add below said lines before curl_exec()
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
So i figured out, i had a space in headers array in KeyValue pair.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type : application/json', ..)
it should have been
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',

PHP PayPal REST API authorization call

I'm trying to use PHP and cURL to request an auth token from PayPal to create a payment.
my code is:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Accept-Language: en_US'
));
curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
$output = curl_exec($ch);
?>
I get the response:
curl_setopt() expects parameter 2 to be long, string given in Users/anth/sites/abyss/auth.php on line 11
{"error":"invalid_client","error_description":"Invalid client credentials"}
line 11 is:
curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx');
I have checked my Client ID and Secret and they are fine.
Does anyone know if I am using the wrong syntax or something?
Thanks
You have used the curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx') instead of curl_setopt($ch, CURLOPT_USERPWD, 'XXXX:XXXX'); . Note the missed 'E' and apart from that you need to add one more line :
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
So the corrected version is below :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Accept-Language: en_US'
));
curl_setopt($ch, CURLOPT_USERPWD, 'XXXX:XXXX');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
print_r($output);

how to read a response back from URL which is sending the response in json

I have been given an API url feed which returns response in JSON format and I have been told to set the headers to this:
Accept: application/json
X-some-API-Key: fdfdfdfdsgddc43aa96c556eb457b4009
Could anyone please tell me how to move ahead with this problem?
Question 1
I would use the PHP curl libraries.
For example:
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'X-some-API-Key: fdfdfdfdsgddc43aa96c556eb457b4009',
));
// grab URL and pass it to the browser
echo curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
See curl_setopt() for more information on the constants such as CURLOPT_HTTPHEADER I have used above.
Question 2 from comments
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'X-some-API-Key: fdfdfdfdsgddc43aa96c556eb457b4009',
));
// grab URL and pass it to the browser
$json = json_decode(curl_exec($ch), true);
// close cURL resource, and free up system resources
curl_close($ch);
$json now contains an associative array of the response, which you can var_dump()to see the structure.
If you're using cURL in PHP you can do set custom headers on the request with:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'X-some-API-Key: fdfdfdfdsgddc43aa96c556eb457b4009'
));

Categories