trying to send post request to api, to get an image back.
example url:
https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php?user=1003123&format=png&cloudid=asdasdasd&pass=123123123
the above url works fine in the browser,
the api doesnt care if the request is get/post,
result of my code is always 'invalid input'.
code:
$url='https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php';
$u = rand();
$p = rand();
$fields = array(
'user'=> urlencode($u),
'pass'=> urlencode($p),
'format'=> urlencode('jpg'),
'cloudid' => urlencode('test')
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
on a side note: is there a way to debug the request in order to see what is being sent ?
The URL provided isn't working for POST request. Here is resulting screenshot (I tried using Advance Rest Client)
However Its working perfectly with GET method. So you can continue using GET request method to generate QR code.
I agree that GET isn't much secure compare to POST method but in your case while requesting from curl user won't get to know about such URL parameters (userid, password). Because curl request will be sending from your web server and not from client/user's browser.
Later you can just output the response image you got from the api.
Related
How can I send a pageview event via Measurement Protocol to a GA4 property with PHP?
This is how I'm doing, but inside my Google Analytics 4 property I can't see any traffic.
$data = array(
'api_secret' => 'XXXX-YYYYY',
'measurement_id' => 'G-12345678',
'client_id' => gen_uuid(), // generates a random id
'events' => array(
'name' => 'page_view',
'params' => array(),
)
);
$url = 'https://www.google-analytics.com/mp/collect';
$content = http_build_query($data);
$content = utf8_encode($content);
$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_POST, TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
I'm working on registering pageviews to track API usage right now, here's what I've found:
XTOTHEL is right about setting the content type to content/json above. In addition to specifying the content type you also have to send JSON data as the CURLOPT_POSTFIELDS data.
Also per their specification the api_secret and measurement_id need to be part of the URI: https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events?client_type=gtag#required_parameters
Lastly, you can use debug mode to validate your responses and figure out what's going on now by simply changing the URL to google-analytics.com/debug/mp/collect
Here's the code I'm working with right now:
//retrieve or generate GA tracking id
if (empty($_COOKIE['_cid'])) {
setcookie('_cid', vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4)));
}
$data = '{"client_id":"'.$_COOKIE['_cid'].'","events":[{"name":"load_endpoint","params":{"page_location":"'.$request->fullUrl().'"}}]}';
echo '<pre>';
print_r($data);
$measurement_id = 'G-xxxxx';
$api_secret = 'xxxx';
$url = 'https://www.google-analytics.com/debug/mp/collect?api_secret='.$api_secret.'&measurement_id='.$measurement_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
This works to a certain extent. Currently it's registering the page view as a custom event instead of an actual pageview though. I'm still trying to figure out how to get them to come through as page views.
Follow up
After a little more debugging I figured out page views are actually working, they just weren't showing up in some of the views. The fix for that was to add page_title into the params:
$data = '
{
"client_id": "'.$_COOKIE['_cid'].'",
"events": [
{
"name": "page_view",
"params": {
"page_location": "'.$request->fullUrl().'",
"page_title": "'.$request->path().'"
}
}
]
}
';
A few extra notes for whoever comes next:
Debug mode did return some useful validation errors for invalid top-level parameters (client_id, events) - but it didn't return errors for anything inside of the "params" for events. IE - i put "page_asdtitle" instead of "page_title" and it accepted it just fine.
None of the tests I sent through actually showed up in the debug panel while using debug mode. I suspect this is because of the data propagation delay, it's probably not loading realtime data.
Using a JSON validator can help. Make sure you use objects and arrays where GA tells you to.
If you get stuck figuring out why your PHP code doesn't work, write the code as a browser event in JavaScript and run it in your browser. There's tons of examples on how to do that. From there, you can use Dev Tools -> Network to inspect the request. If you right click on the google analytics request to the 'collect' endpoint you'll see an option to Copy Request as CURL. Put that into a text editor and compare it to what your PHP code is sending.
To ACTUALLY test this without the massive propagation delay you can login to Google Analytics, go to Reports -> Realtime, and you should see your data show up within 30-60 seconds if it's working. Realtime data will NOT show up if you're using the /debug/ endpoint though.
I'm sending API request using curl and php. The first one works but not the second one. Error shows that postfields data is empty, or the whole request is null. I'm confused.
this is my code
$request_id = "aaa7996d-8d5c-4116-b759-6afb1c84ff39";
$res_url = "http://opeapi.ws.pho.to/getresult";
$res_data = array('request_id'=>$request_id);
$res_ch = curl_init();
curl_setopt($res_ch, CURLOPT_URL, $res_url);
curl_setopt( $res_ch, CURLOPT_POST, true );
curl_setopt($res_ch, CURLOPT_POSTFIELDS,$res_data);
curl_setopt($res_ch, CURLOPT_RETURNTRANSFER, 1);
$results = curl_exec($res_ch);
curl_close($res_ch);
var_dump($results);
but the results returned:
SecurityError612Bad, invalid or empty REQUEST_ID parameter.
Here are a few things I tries:
$res_data = array('request_id'=>urlencode($request_id));
$res_data = "request_id=".$request_id;
$res_data1 = json_encode($res_data);
$res_data1 = http_build_query($res_data);
$res_url = "http://opeapi.ws.pho.to/getresult?request_id=aaa7996d-8d5c-4116-b759-6afb1c84ff39";
none of them really works.
but the curious thing is I have another request before this one. Also using Pho.to API. I processed a photo using their parameters and got the result using curl and php. That one works (that's why I get the request_id to obtain the processing result)
this is my former request. it works, why not the second one??
$data = '(some xml parameters)';
$sign_data = hash_hmac('SHA1', $data,'***');
$url = "http://opeapi.ws.pho.to/addtask";
$posting = array('app_id'=>'***','key'=>'***','sign_data'=>$sign_data,'data'=>$data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS,$posting);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
error message suggest there is no request_id...
or the whole request $curl is null...
Any help or suggestion will be deeply appreciated!!! Please~Thanks!!!
I figured it out!
It turns out I'm supposed to use a get request (in order to get my data), not a post.
So I directly write my url to...http://?request_id=, and deleted all the option lines about request (because the default process is get, not post), and it worked!
took me 7 hours to figure this out...TAT
I try to get odata produced by a navision webservice. When I directly access the url given using chrome browser, the page asks for user name and password and then chrome shows xml data as expected.
But when i use a PHP script, it always returns "1".
My code looks like this:
$url = 'http://103.7.1.182:14048/DynamicsNAV71-6/OData/Company(\'Unit%20G%205\')/Item_Master_on_hand_no_Desc';
$login = 'Gem-gae\senzo1:Bsbsenzo2018';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $login);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/x-www-form-urlencoded',
'Content-Length: ' . strlen(1))
);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
Is there any mistakes with that code ?
Thanks for your help
There's no mistake on your code, it successfully connects to the external server via cURL and retrieves the data.
The value in $output (if you do a var_dump) is true (that's why you see 1 if you echo it).
Said that, the issue is on the server you are contacting (103.7.1.182:14048).
You probably need to send some data or something, I can't tell you exactly what because I don't know what is in that server.
To send data, you can add a curl_setopt line:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
Where $data is an array.
I have a API url for sending SMS and I have to execute this url once i create a new user. Using PHP how can i call the url in background?
I have tried file_get_contents()
My url is like this
http://bulksms.mysmsmantra.com/WebSMS/SMSAPI.php?username=hidden&password=hidden&sendername=iZycon&mobileno=8443223
While using file_get_contents() im getting a 'Bad request' due to the change in url where all the '&' got replaced using '&'. this replaces the username and password which passed to the url.
And also tried some other functions like fopen() curl() . unfortunately for all this function im facing the same issue.
So which is the proper method for calling this kinda URL.?
thanks in advance.
Perhaps try executing it like so:
<?php
$url = 'http://bulksms.mysmsmantra.com/WebSMS/SMSAPI.php';
$fields = array(
'username' => "hidden",
'password' => "hidden",
'sendername' => "iZycon",
'mobileno' => 8443223
);
//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, http_build_query($fields));
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
var_dump($result);
i wrote a script, that should call a simple soap service (in python) to receive datas. The url requires get parameters, to work fine.
I realized the request with php curl:
<?php
// Curl Init
$ch = $curlObject;
// Set URL
$url = "http://localhost/soapService/?";
// Add get params
$url .= http_build_query(array("article" => "[($articleNR, 5)]"));
// Fill curl
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute
$serverOutput = curl_exec($ch);
?>
But the response says:
"NameError: name 'article' is not defined"
And if i call the URL directly in my browser, everything works fine. Where is the magic?
Ah, just made it! There was a missing CURL PARAM
curl_setopt($ch, CURLOPT_HTTPGET, true); // Now everything works fine!