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);
Related
I am trying to fetch data from webservice(nodejs) using curl in php but i am getting result "lang is required"
i tried with following code but not working for me where i am wrong ?
Here is my code
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init();
$url="http://xxxxxx:8000/api/employer/login";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
Usually to send post variables i use : the http_build_query function.
$url = "http://xxxxxx:8000/api/employer/login";
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
It works better.
first check the request whether send post field or not.
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init();
$url="https://httpbin.org/post";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
the site: https://httpbin.org/ will response the post field if the post data exists.
I think the problem may occurs the nodejs side. Does it check the post field rightly ?
from my experience your code is right.
Hello i want to convert this command line in php curl
curl -X POST https://api.imgur.com/3/upload -H "Authorization: Client-ID cdeebfbfe0d825a" -F "image=http://IP/output/1530096054.png"
MY PHP code
$URL = 'https://api.imgur.com/3/upload';
$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);
print_r($ch);
print_r($status_code);
Thanx for help
I found the solution
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/upload');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'image=http://IP/output/1530096054.png');
$headers = array();
$headers[] = 'Authorization: Client-ID 46as4d6as54d6as46';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
I have the following post api request, the example provided was using cURL command:
curl "https://muserver.com/api/gettoken" --request POST --include -
-header "Content-Type: application/json" --user test:test
I am trying to perform this request using cURL in PHP but getting authentication error.
following is what I have tried:
ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
I think I should pass the user and pass. in the form of
--user test:test
but really don't know how to do it.
You seem to be lacking the $username and $password variables :
$username='test';
$password='test';
ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
<?php
$url_simple_auth = 'https://www.some_url_need_basic_auth_first.com';
$url_download = 'https://www.some_url_need_basic_auth_first.com/dowonload_file.zip';
$username = 'some_login';
$password = 'some_pass';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_simple_auth);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// login simple auth
$simple_auth = curl_exec($ch);
// download file
curl_setopt($ch, CURLOPT_REFERER, $url_simple_auth);
curl_setopt($ch, CURLOPT_URL, $url_download);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "email=$username&key=$password");
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($status_code);
var_dump($result);
I have command
curl -v -X POST -H "Content-Type: application/xml" -d #case.xml --user test:test url
in php send
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_USERPWD, "test:test");
curl_setopt($ch, CURLOPT_POSTFIELDS, ['data' => new CurlFile('case.xml')]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
but this code not work, error 400 Bad Request.
The web server uses Basic HTTP Authentication. I'm using 7.0.11
<?php
$login='test';
$password='test';
$xml_data =file_get_contents(realpath('case.xml'));
$url ='mega_url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$output = curl_exec($ch);
it is work
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);