Execute PHP using Curl - php

I want to exceute this using php curl
[ ~ ] $ curl -u cbb8f088775cf209035729c0eb69b9f340a3b047:X https://subs.pinpayments.com/api/v4/meresheep/subscribers.xml
So far, I tried
// Query the user to pin payments for the details...
$curl_url = "https://subs.pinpayments.com/api/v4/rigid-test/subscribers/rigids-12345.xml";
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: -u cbb8f088775cf209035729c0eb69b9f340a3b047'));
// curl_setopt($data, CURLOPT_HTTPHEADER, array(
'Authorization: Basic ' . base64_encode('cbb8f088775cf209035729c0eb69b9f340a3b047')
));
//execute post
echo $result = curl_exec($ch);
echo curl_error($ch);
die('qqqqqqqqq');
curl_close($ch);

Mistakenly the variable used was wrong.
Here is the code again.
// Query the user to pin payments for the details...
$curl_url = "https://subs.pinpayments.com/api/v4/rigid-test/subscribers/12345.xml";
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_USERPWD,"b59d75bbdead46741b3b6875fc4b50e698392d71:test4321");
//execute post
$result = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);

Related

authenticate through curl and get resource [duplicate]

I am having problem with PHP curl request with basic authorization.
Here is the command line curl:
curl -H "Accept: application/product+xml" "https://{id}:{api_key}#api.domain.com/products?limit=1&offset=0"
I have tried by setting curl header in following ways but it's not working
Authorization: Basic id:api_key
or
Authorization: Basic {id}:{api_key}
I get the response "authentication parameter in the request are missing or invalid" but I have used proper id and api_key which is working in command line curl (I tested)
Please help me.
Try the following code :
$username='ABC';
$password='XYZ';
$URL='<URL>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close ($ch);
Can you try this,
$ch = curl_init($url);
...
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
...
REF: http://php.net/manual/en/function.curl-setopt.php
$headers = array(
'Authorization: Basic '. base64_encode($username.':'.$password),
);
...
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
Its Simple Way To Pass Header
function get_data($url) {
$ch = curl_init();
$timeout = 5;
$username = 'c4f727b9646045e58508b20ac08229e6'; // Put Username
$password = ''; // Put Password
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); // Add This Line
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = "https://storage.scrapinghub.com/items/397187/2/127";
$data = get_data($url);
echo '<pre>';`print_r($data_json);`die; // For Print Value
Check My JSON Value

how to convert curl into php cURL

I have referenced and tried suggestions from previous stackoverflow questions to try and convert this type of CURL request to php but failed: Bad request returned. I have also read up on it and tried the following as well as other attempts:
This type of request works:
$ curl -X POST \
-u f98b80bd3aa9dd8:748b2a74-cf59-4afb-facf-b79f3ba51e46 \
-H 'Content-Type:application/json' \
-d '{"hash": "fa814e37ad092518b0b22ed1f21c8bac4daed663433abba01e369399522279e6"}'
https://api-prod.stamp.com/stamps
My clearest attempts:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-prod.stamp.com/stamps');
curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_USERPWD, 'f98b80bd3aa9dd8:748b2a74-cf59-4afb-facf-b79f3ba51e46');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"hash":"fa814e37ad092518b0b22ed1f21c8bac4daed663433abba01e369399522279e6"}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
Also tried:
$url = 'https://api-prod.stamp.com/stamps';
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type: application/json'));
$json = json_encode('{f98b80bd3aa9dd8:748b2a74-cf59-4afb-facf-b79f3ba51e46"}');
curl_setopt($ch,CURLOPT_POSTFIELDS, $json);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
Updated on suggestion from cBroe, improved but still not right: Update
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-prod.stamp.com/stamps');
curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type:application/json'));
curl_setopt($ch,CURLOPT_USERNAME, 'f98b80bd3aa9dd8');
curl_setopt($ch, CURLOPT_USERPWD, '748b2a74-cf59-4afb-facf-b79f3ba51e46');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"hash":"fa814e37ad092518b0b22ed1f21c8bac4daed663433abba01e369399522279e6"}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);

Google maps api trying to get address using place id in hindi

I'm trying to get address using place_id in hindi but I'm getting some special characters. Please someone help me.
<?php
$url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJLbZ-NFv9DDkRzk0gTkm3wlI&language=hi&key=AIzaSyBt7SRPMKW4pWc06NwuzFNCErXxnIlyEIY';
$headers = array(
'Content-Type: application/json'
);
// OPEN CONNECTION
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if(curl_errno($ch)){
echo curl_error($ch);
}
// Close connection
curl_close($ch);
$result=json_decode($result);
//pr($result);
echo "<pre>";print_r($result->result->formatted_address);
?>

Camfind API issue in PHP

For anyone familiar with the Camfind API, I've got a bit of a stumper today Implementing the API in PHP. I've followed the instructions as best I could, and I'm getting the proper token back for my first call. I'm then appending the token to the URL, removing the form data, and keeping the json header, as well as the header with my key. Whatever I try, however, returns: array(1) { ["status"]=> string(13) "not completed" }
Here's my code:
<?php
///////////////////////////////////
// FIRST REQUEST
///////////////////////////////////
//set POST variables
$url = 'https://camfind.p.mashape.com/image_requests';
$fields = array(
"image_request[altitude]" => "27.912109375",
"image_request[language]" => "en",
"image_request[latitude]" => "35.8714220766008",
"image_request[locale]" => "en_US",
"image_request[longitude]" => "14.3583203002251",
"image_request[remote_image_url]" => "https://www.google.com/images/srpr/logo11w.png"
);
$headers = array(
'X-Mashape-Key: I-put-my-real-key-here',
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json'
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$result = json_decode($result, true);
///////////////////////////////////
// SECOND REQUEST
///////////////////////////////////
//set POST variables
$url = "https://camfind.p.mashape.com/image_responses/" . $result['token'];
echo $url;
$headers = array(
'X-Mashape-Key: I-put-my-real-key-here',
'Accept: application/json'
);
//open connection
$ch = curl_init();
//set the url
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
var_dump(json_decode($result, true));
Help appreciated!
Camfind support just got back to me, and made the following suggestion "Hi, it takes around 10-15 seconds for the image recognition to occur. Build in a loop with a delay before making the second call with the token, or try calling again and use a timer to re-call if not completed is still returned until it changes status to completed."
With this advice, I've updated my code as follows:
I added:
$start = microtime(true);
To the top of my file. I then wrapped my second curl call like so:
do {
//set the url
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 30);
$result = json_decode(curl_exec($ch), true);
} while((number_format((microtime(true) - $start), 2) < 20) && $result['status'] == 'not completed');
This is the solution, and the search now executes correctly.

How to use basic authorization in PHP curl

I am having problem with PHP curl request with basic authorization.
Here is the command line curl:
curl -H "Accept: application/product+xml" "https://{id}:{api_key}#api.domain.com/products?limit=1&offset=0"
I have tried by setting curl header in following ways but it's not working
Authorization: Basic id:api_key
or
Authorization: Basic {id}:{api_key}
I get the response "authentication parameter in the request are missing or invalid" but I have used proper id and api_key which is working in command line curl (I tested)
Please help me.
Try the following code :
$username='ABC';
$password='XYZ';
$URL='<URL>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close ($ch);
Can you try this,
$ch = curl_init($url);
...
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
...
REF: http://php.net/manual/en/function.curl-setopt.php
$headers = array(
'Authorization: Basic '. base64_encode($username.':'.$password),
);
...
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
Its Simple Way To Pass Header
function get_data($url) {
$ch = curl_init();
$timeout = 5;
$username = 'c4f727b9646045e58508b20ac08229e6'; // Put Username
$password = ''; // Put Password
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); // Add This Line
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = "https://storage.scrapinghub.com/items/397187/2/127";
$data = get_data($url);
echo '<pre>';`print_r($data_json);`die; // For Print Value
Check My JSON Value

Categories