I am using curl to perform a request to the Twilio Verify API, following the instructions here: https://www.twilio.com/verify/api
Using these instructions, I've created two php files to perform the curl request -- one to get a verification code (get_code.php), and another to check the verification code (check_code.php). These scripts are called using an ajax post to send the parameters, and the two scripts are nearly identical, save for the URL ("/start" vs. "/check").
I believe I am specifying the correct URLs, and get_code.php works, but check_code.php throws the following error:
Requested URL was not found. Please check http://docs.authy.com/ to see the valid URLs.
get_code.php
<?php
$USER_PHONE = htmlspecialchars($_POST["phone"]);
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://api.authy.com/protected/json/phones/verification/start",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'country_code' => '1',
'via' => 'sms',
'phone_number' => $USER_PHONE,
),
CURLOPT_HTTPHEADER => array('X-Authy-API-Key: MY_KEY')
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
check_code.php
<?php
$USER_PHONE = htmlspecialchars($_POST["phone"]);
$VERIFY_CODE = htmlspecialchars($_POST["code"]);
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://api.authy.com/protected/json/phones/verification/check",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'country_code' => '1',
'phone_number' => $USER_PHONE,
'verification_code' => $VERIFY_CODE
),
CURLOPT_HTTPHEADER => array('X-Authy-API-Key: MY_KEY')
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
I performed a curl manually in terminal using the same URL and parameters, and it worked.
curl "https://api.authy.com/protected/json/phones/verification/check?phone_number=MY_PHONE&country_code=1&verification_code=MY_CODE" -H "X-Authy-API-Key: MY_KEY"
I don't know what I could be doing wrong?
OK, well I have no idea why this worked, but I got it working and maybe someone else can explain why. I built the CURL URL as a string and I removed the CURLOPT_RETURNTRANSFER and CURLOPT_POST arguments.
<?php
$USER_COUNTRY = "1";
$USER_PHONE = htmlspecialchars($_POST["phone"]);
$VERIFY_CODE = htmlspecialchars($_POST["code"]);
$URL = "https://api.authy.com/protected/json/phones/verification/check?country_code=1&phone_number=".$USER_PHONE."&verification_code=".$VERIFY_CODE;
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => $URL,
CURLOPT_HTTPHEADER => array('X-Authy-API-Key: MY_KEY')
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
🤷
Related
How to send two request with curl using the same headears and params, only the url change
I dont want to duplicate the code two time
$url1 = "www.example1.com"
$url2 = "www.example2.com"
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $config->url1 . '/api/v1/orders',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
Thank you
You can put everything into a function and call that:
function get_orders($url)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url . '/api/v1/orders',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Call with
$url1 = get_orders('https://example1.com');
$url2 = get_orders('https://example2.com');
Kind of basic:
$urls[] = "https://www.example1.com";
$urls[] = "https://www.example2.com";
foreach ($urls as $url){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url . '/api/v1/orders',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
}
I see a function option was also answered! :) this one is with a loop.
am trying to login to netflix using php curl but i get no response at all or errors.here is my code
$handle = curl_init();
$url = "https://www.netflix.com/ke-en/login";
// Array with the fields names and values.
// The field names should match the field names in the form.
$postData = array(
'userLoginId' => 'Lady',
'password' => 'Gaga',
'submit' => 'ok'
);
curl_setopt_array($handle,
array(
CURLOPT_URL => $url,
// Enable the post response.
CURLOPT_POST => true,
// The data to transfer with the response.
CURLOPT_POSTFIELDS => $postData,
CURLOPT_RETURNTRANSFER => true,
)
);
$data = curl_exec($handle);
echo curl_error($handle);
echo curl_errno($handle);
curl_close($handle);
echo $data;
the error i get is 0 and i saw that it means it is successfull
Here is my code.
<?php
if(isset($_REQUEST['name']) and ($_REQUEST['email']) and ($_REQUEST['msg'])){
$name=$_REQUEST['name'];
$email= $_REQUEST['email'];
$msg = $_REQUEST['msg'];
$url="http://14.140.111.4:20002/ContactUs?name=$name&Email=$email&Message=$msg";
$url = urlencode($url);
$curl = curl_init();
// Set some options - we are passing in a useragent too here urlencode
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Cubewires Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'name'=>$name,
'Email' => $email,
'Message'=>$msg
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
print_r($resp);
// Close request to clear up some resources
curl_close($curl);
}
?>
i got response HTTP Error 400. while using this API data should be inserted in database.
Thanks,
vivek
I'm using the LinkedIn REST API to post updates to a users timeline.
Since a few days I get an Internal server error response from LinkedIn but the code worked before.
PHP:
$postTitle = "hello";
$postDesc = "world ";
$submitted-url = "http://example.com";
$submitted-image-url = "http://images.example.com/img/hello.jpg";
$comment = "foobar";
$postData = array('visibility' => array('code' => 'connections-only'),
'content' => array('title' => $postTitle,'description' => $postDesc,'submitted-url' => $postURL,'submitted-image-url' => $postImage), 'comment' => $postComment);
$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?oauth2_access_token='.$oauthToken.'&format=json'
);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"),
CURLOPT_POSTFIELDS => json_encode($postData)
));
$response = curl_exec($ch);
How to fix that error?
Your code is invalid PHP (perhaps because of some edits you made before posting?); modifying it to:
$postTitle = "hello";
$postDesc = "world ";
$postURL = "http://example.com";
$postImage = "http://images.example.com/img/hello.jpg";
$postComment = "foobar";
$oauthToken = "<token>";
$postData = array(
'visibility' => array('code' => 'connections-only'),
'content' => array(
'title' => $postTitle,
'description' => $postDesc,
'submitted-url' => $postURL,
'submitted-image-url' => $postImage
),
'comment' => $postComment
);
$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?oauth2_access_token='.$oauthToken.'&format=json');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"),
CURLOPT_POSTFIELDS => json_encode($postData)
));
$response = curl_exec($ch);
works if only $oauthToken is set to a valid token. Assuming your real code is correct the only possiblity left is that your OAuth token has expired and you need to obtain a new one first. By adding CURLOPT_VERBOSE => TRUE to the cURL options you would find out more about the error that LinkedIn returns.
You may considering using the LinkedIn PHP SDK (provided by the community) instead: https://github.com/Happyr/LinkedIn-API-client
We faced similar issue with Linkedin API recently. Finally figured out the fix by changing the url.
New URL : "https://api.linkedin.com/v1/people/~/shares"
Instead of specifying 'oauth2_access_token' in the query string,
add it in the header - specify :
"Authorization", "Bearer " + accessToken.
And finally in the request body parameter, add your json/xml data to post
You have to Use your Authentication Token in Request Headers.
This is the working code. Try it.
$postTitle = "hello";
$postDesc = "world ";
$submitted-url = "http://example.com";
$submitted-image-url = "http://images.example.com/img/hello.jpg";
$comment = "foobar";
$oauthToken = "TokenHere";
$postData = array('visibility' => array('code' => 'connections-only'),
'content' => array('title' => $postTitle,'description' => $postDesc,'submitted-url' => $postURL,'submitted-image-url' => $postImage), 'comment' => $postComment);
$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?format=json');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json", "Bearer: ".$oauthToken.""),
CURLOPT_POSTFIELDS => json_encode($postData)
));
$response = curl_exec($ch);
I'm new to php and I'm trying to create a simple example for calling our company api. I got NetBeans IDE 7.1.2 to work last night (yay) but I cannot seem to get the following code to show me anything. I can run it in the debugger. I can step through it. I even get to the end without errors, but the curl_exec returns just 0. I have added the CURLOPT_PROXYPORT so that I can get fiddler to see the traffic, but fiddler sees nothing. I am also trying to run this as a php command line (if that has any bearing).
I know I'm doing something stupid... but that's the problem with stupid.
<?php
$url = 'https://target.boomerang.com/api/JobCreate';
$authToken = 'phptest';
$data = array(
"emailHTML" => "Howdy",
"jobKind" => "email",
"senderEmail" => "bob#boomerang.com",
"subject" => "My howdy email"
);
$data_string = json_encode($data);
$headers = array(
'Content-type: application/json',
'auth_token: ' . $authToken,
'Accept: application/json',
'Expect:'
);
$ch = curl_init();
$args = array(
CURLOPT_URL => $url,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_PROXYPORT => "localhost:8888"
);
curl_setopt_array($ch, $args);
$res = curl_exec($ch);
$res_data = json_decode($res, true);
print($res_data);
?>
Thanks for any help.
So the problem was caused by SSL certificate mismatch? I suppose it can be solved by adding this...
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
...into $args array.