POST data for url with cuRL - php

I am trying to send some data to an curl URL to obtain a key.
The curl command is:
curl -X POST "http://website.com/api/open/oauth/token" -H "accept: application/json" -H "Authorization: xx:xxxxxxxxxxxxxxxx" -H "content-type: application/x-www-form-urlencoded" -d "grant_type=password&username=xxx#yahoo.com&password=xxxx"
Any idea how this will look into a php code to get the response key ?

You can try like this,
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://website.com/api/open/oauth/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=password&username=xxx#yahoo.com&password=xxxx");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Authorization: xx:xxxxxxxxxxxxxxxx";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Using cURL, you can do something like this:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://website.com/api/open/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=password&username=xxx%40yahoo.com&password=xxxx",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: xx:xxxxxxxxxxxxxxxx",
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"postman-token: ac46b74a-6b24-85a4-36ea-4867a3bd3eda"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
You can also use the HttpRequest class:
<?php
$request = new HttpRequest();
$request->setUrl('http://website.com/api/open/oauth/token');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'postman-token' => '67cba408-41b5-7c86-568f-437e27abfa08',
'cache-control' => 'no-cache',
'content-type' => 'application/x-www-form-urlencoded',
'authorization' => 'xx:xxxxxxxxxxxxxxxx',
'accept' => 'application/json'
));
$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array(
'grant_type' => 'password',
'username' => 'xxx#yahoo.com',
'password' => 'xxxx'
));
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}

Related

Pass Params in Curl Request

I have try curl request with terminal it's working but when i convert that curl request into php code that one passing param not working.
Terminal curl request :
curl --insecure "https://www.zohoapis.in/phonebridge/v3/clicktodial" -X POST -d "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456" -H "Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf" -H "Content-Type: application/x-www-form-urlencoded"
Response :
{"message":"ASTPP Clicktodial functionality has been enabled","status":"success","code":"SUCCESS"}
PHP Code :
$zohouser = '6000';
$access_token = '1000.c3c1107b635f1f5b257d831677e077d2';
$cURL = "https://www.zohoapis.in/phonebridge/v3/clicktodial?clicktodialuri=$click_to_dial&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=$zohouser";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $cURL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Zoho-oauthtoken " . $access_token,
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
),
));
$response = json_decode(curl_exec($curl));
$err = curl_error($curl);
print_r($err);
curl_close($curl);
print_r($response);exit;
Not getting any response or error during run this php curl request.
Can you please help me how to pass string as param in php curl request.
$strURL= "https://www.zohoapis.in/phonebridge/v3/clicktodial";
$arrHeader= array(
'Authorization:Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf'
);
$params= array(
"clicktodialparam"=>"[{\"name\":\"fromnumber\",\"value\":\"555\"}]",
"authorizationparam"=>"{\"name\":\"X-Auth-Token\",\"value\":\"1000.aedb399e2389cfacef60f965af052cbf\"}",
"clicktodialuri" => "$click_to_dial",
"zohouser" => "123456"
);
$ch= curl_init();
curl_setopt_array($ch,array(
CURLOPT_URL =>$strURL,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $arrHeader,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 0,
CURLOPT_TIMEOUT => 0,
CURLOPT_POSTFIELDS => http_build_query($params)
));
$strResponse= curl_exec($ch);
print_r($strResponse);
echo curl_error($ch);
Referring to this post and using the linked tool we end up with the following code. Since I cannot test this myself I cannot guarantee my answer. It looks like you might be missing the curl post option "curl_setopt($ch, CURLOPT_POST, 1);" which you can use in lieu of the option you used "CURLOPT_CUSTOMREQUEST => "POST"".
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.zohoapis.in/phonebridge/v3/clicktodial');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456");
$headers = array();
$headers[] = 'Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);

PHP Send Request Header not Sent Response NULL

i have a problem when send request header to my restful, my restful checking request Authorize header, but when i send the header is missing.
My restful debug give me result NULL
i was tried to add CURLOPT_SSL_VERIFYPEER but also not working ,
anyone can help me out ?
here is my code :
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => "https://mysitehttps.domain",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"asdasd\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Authorization: 123HaHaHa",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"postman-token: 3b3fd06d-a8aa-65db-a917-c911fa0bb5d5"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Thank you
I have tested this code and its working for me.
Your content-type is set to multi-part. Are you sure its not json. anyway try the code below
//initialize data variables that you want to send as post below
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('https://mysitehttps.domain');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
"Authorization: 123HaHaHa",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"postman-token: 3b3fd06d-a8aa-65db-a917-c911fa0bb5d5"
)
);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

How to fetch particular data from api using php

I am new in fetching data using APIs and need to fetch some particular columns data from a number of columns available. I am able to fetch all the data using API but wondering how to fetch particular columns. Below is my code:
<?php
$url = 'myurl';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"username\": \"test\", \"password\": \"test\"}",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
$request = curl_init();
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($request, CURLOPT_HEADER, FALSE);
$result = curl_exec($request);
curl_close($request);
if ($result) {
$data = new SimpleXMLElement($result);
foreach ($data->Item as $entry) {
echo "XML Activity Name: " . $entry->ActivityName . "<br>";
}
} else {
exit();
}
?>

CURL response Empty reply from server

I am using native php 5 (I know it sucks). I want to use curl post with some response
{ SUCCESS = 0,UNAUTHORIZED = 4,WRONG_PARAM = 5,DTIME_OLD = 6,WRONG_SIGN = 11,TOO_LONG = 12}
Actually i've tried on postman and it works with return 0 (SUCCESS).
But when I try on php 5 on localhost XAMPP, it always return empty reply from server.
I also check it on verbose cmd and it still no reply (img:https://i.stack.imgur.com/1zuIx.png).
Any idea, please?
Here is the code:
$path = "msisdn=087884327525&text=meong&sign=".$sign."&userid=19348&timestamp=".$date_fix;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('accept: /User-Agent: python-requests/2.8.1',
'accept-encoding: gzip, deflate',
'cache-control: no-cache',
'connection: keep-alive',
'content-length: 119',
'Content-Type: application/x-www-form-urlencoded; charset=utf-8'
// 'postman-token: 53c6e053-1692-010b-ffb9-b249ab94fca1'
));
$response = curl_exec($ch);
if ($response === false){
print_r('Curl error: ' . curl_error($ch));
}else{
echo "success";
}
curl_close($ch);
print_r($response);
ANSWER
I change the 'accept' header and separate it with 'user-agent'. And also, I move the timestamp path to prevent error '&times' into 'x'. I ran it on postman (works), then I copy the code from postman to my editor, and it works.
Here is the code:
$curl = curl_init();
curl_setopt_array($curl, array(
// CURLOPT_PORT => "9922",
CURLOPT_URL => $host,
CURLOPT_RETURNTRANSFER => true,
// CURLOPT_ENCODING => "",
// CURLOPT_MAXREDIRS => 10,
// CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false ,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "timestamp=".$date_fix."&msisdn=087881257525&text=test%20USSD&sign=".$sign."&userid=19348",
CURLOPT_HTTPHEADER => array(
"accept: /",
"accept-encoding: gzip, deflate",
"cache-control: no-cache",
"connection: keep-alive",
"content-type: application/x-www-form-urlencoded",
"postman-token: 95239f79-13c2-f64e-4fa0-d2498e5118c9",
"user-agent: python-requests/2.8.1"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

How to request this in phpcurl

$url = "http://ups.youku.com/ups/get.json?vid=XMTQ4ODM5Mjk2MA==&ct=10&ccode=0502&client_ip=0.0.0.0&utid=Ga3jEdWulXoCAXZwOs6IYOEY&client_ts=1501211617"
I try to request this by postman,add a header( Referer: http://static.youku.com/ ), it works.
But when I use CURL,it always show me nothing.I am not sure if cookies affect on this because I clear all the cookies on chrome,the postman still works.
I can't get useful cookies for this request by CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE,any body help me pls,thanks.
<?php
header("Content-type: text/html; charset=utf-8");
$headers = array(
'Referer' => 'http://static.youku.com/'
);
$url = 'http://ups.youku.com/ups/get.json?vid=XMTQ4ODM5Mjk2MA==&ct=10&ccode=0502&client_ip=0.0.0.0&utid=Ga3jEdWulXoCAXZwOs6IYOEY&client_ts=1501211617';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($curl);
curl_close($curl);
?>
Thks for all,try to add params of postman into curl,this way works.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ups.youku.com/ups/get.json?vid=XMTQ4ODM5Mjk2MA%3D%3D&ct=10&ccode=0502&client_ip=0.0.0.0&utid=Ga3jEdWulXoCAXZwOs6IYOEY&client_ts=1501211617",
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: 88976d57-03fd-7950-6c65-9d0f191010dc",
"referer: http://static.youku.com/";
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

Categories